Changing Single Quotation Marks to Double in eBooks

As a person living in the USA I highly prefer double quotation marks to single quotes when denoting speech. Some authors use the single quote for effect but mostly it’s just a style choice. I find UK authors generally use the two interchangeably. Tolkien books are a good example. I have The Hobbit, The Lord of the Rings, and The Children of Hurin. The Hobbit use double quotes while the other two uses single quotes.

Following is some simple python code that will take the book (named th.txt) and change the single quotes into double quotes for the books in question. Both use ’ and ’ for the opening and closing quotes. Also ’ is used for contractions. The regexes take the opening and closing characters into account as well as change the contractions to the non-unicode ‘ character.

>>> th = open('th.txt', 'rb+wb')
>>> th_t = th.read()
>>> th_t = re.sub('(?u)(?>> th_t = re.sub('(?u)‘', '"', th_t)
>>> th_t = re.sub('(?u)’', '"', th_t)
>>> th.seek(0)
>>> th.truncate(0)
>>> th.write(th_t)

I do realize that the listed regexes could be combined a bit especially the opening and closing quotes. However, that would reduce their readability.