Reputation: 12217
I'm trying to replace an apostrophe in a python unicode string. In the debugger it looks the following:
u'Tu veux aller trop vite! Pour répondre à cette question, tu dois d\\'abord trouver le bon code.'
After the replacement it should have \'
instead of \\'
.
When I try to replace the apostrophe, nothing happens. What should I do?
The following shows plus/minus the code:
def convert(sheet, file):
from xlrd import open_workbook
from lxml import etree
value = sheet.cell(1, 1).value
#value = u'Tu veux aller trop vite! Pour répondre à cette question, tu dois d\\'abord trouver le bon code.'
child = etree.Element('string', name=key.encode('utf-8'))
child.text = value
file.write(etree.tostring(root, encoding='utf-8', pretty_print=True))
Upvotes: 0
Views: 1367
Reputation: 2459
I'm not sure of what you to achieve here. Do you want to replace the '\\' by '\' in a string variable of your python script? As your literal is invalid, I have done a small change.
Here's an idea to replace your '\\' by '\' using a regexp
# -*- coding: utf-8 -*-
import re
str= u"Tu veux aller trop vite! Pour répondre à cette question, du dois d\\'abord trouver le bon code."
str2=re.sub(r'\\', "", str)
print str
print str2
Output:
Tu veux aller trop vite! Pour répondre à cette question, du dois d\'abord trouver le bon code.
Tu veux aller trop vite! Pour répondre à cette question, du dois d'abord trouver le bon code.
Upvotes: 0
Reputation: 6386
Your literal is invalid, you escaped backslash, but doing so failed to escape single quote ('
), thus terminating literal too early. Try those, depending what are you going to achieve:
print u'Tu veux aller trop vite! Pour répondre à cette question, tu dois d\\\'abord trouver le bon code.'
print u"Tu veux aller trop vite! Pour répondre à cette question, tu dois d\\'abord trouver le bon code."
print u'Tu veux aller trop vite! Pour répondre à cette question, tu dois d\'abord trouver le bon code.'
print u"Tu veux aller trop vite! Pour répondre à cette question, tu dois d'abord trouver le bon code."
Upvotes: 1