【发布时间】:2023-03-28 17:24:01
【问题描述】:
我在这样的文本文件中有一些大内容:
1. name="user1” age="21”
2. name="user2” age="25”
....
如果我们注意到我在每个单词的末尾都有这种 (”) 特殊类型的引号。
我只想用普通的引号 (") 替换那个引号 (”)
代码:
import codecs
f = codecs.open('myfile.txt',encoding='utf-8')
for line in f:
print "str text : ",line
a = repr(line)
print "repr text : ",a
x = a.replace(u'\u201d', '"')
print "new text : ",x
输出:
str text : 1. name="user1” age="21”
repr text : u'1. name="user1\u201d age="21\u201d\n'
new text : u'1. name="user1\u201d age="21\u201d\n'
但它不起作用。我在这里缺少什么?
更新:
我刚试过这个:
import codecs
f = codecs.open('one.txt')
for line in f:
print "str text : ",line
y= line.replace("\xe2\x80\x9d", '"')
print "ynew text : ",y
它现在正在工作。
我还是想知道x = a.replace(u'\u201d', '"')出了什么问题
【问题讨论】: