* Changing Single Quotation Marks to Double in eBooks
Posted on May 12th, 2009 by John. Filed under programming.
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.
Leave a Reply
Tags
Archives
- July 2010 (4)
- June 2010 (1)
- May 2010 (2)
- March 2010 (1)
- January 2010 (8)
- December 2009 (5)
- November 2009 (6)
- October 2009 (4)
- September 2009 (2)
- August 2009 (6)
- July 2009 (6)
- June 2009 (4)
- May 2009 (6)
- April 2009 (4)
- March 2009 (2)
- February 2009 (4)
- January 2009 (4)
- December 2008 (7)
- November 2008 (2)