如何用普通引号 (`"`) 替换引号 (`”`)
How to replace quote ( `”` ) with normal quote (`"`)
我在这样的文本文件中有一些大内容:
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', '"')
出了什么问题
a
是该行的 repr,不 包含字符 ”
,但是包含字符串 \
,u
,2
,0
,1
,d
.
因此将 a = repr(line)
更改为 a = line
将解决问题。
我在这样的文本文件中有一些大内容:
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', '"')
a
是该行的 repr,不 包含字符 ”
,但是包含字符串 \
,u
,2
,0
,1
,d
.
因此将 a = repr(line)
更改为 a = line
将解决问题。