Abhishek Bhatia
Abhishek Bhatia

Reputation: 9806

Replace all consecutive single quote pairs with double quotes

I am trying replace all consecutive single quote pairs with double quotes.

Code:

text.replace('\'\'','\"')

But this doesn't seem to work.

Background: I am extracting article text from stored .html files using Goose.

article = extractor.extract(raw_html=html)              #extracts content
text = (unidecode(article.cleaned_text))                  #changes encoding

Here article is in unicode and text in str. I am using Python 2. I try to print the text.

print text

Output:

''Several people were crushed or trampled to death,'' the police said in a statement.

instead of

\'\'Several people were crushed or trampled to death,\'\' the police said in a statement.

This is somewhat confusing to me. My code would work if it was in the second format. I don't understand how the string is stored even given the quotes are not preceeded by \.

Upvotes: 0

Views: 743

Answers (1)

muddyfish
muddyfish

Reputation: 3650

Use text.replace("''",'"')

This works because the double quoted string didn't need to be escaped.

Upvotes: 4

Related Questions