【问题标题】:How to replace quote ( `”` ) with normal quote (`"`)如何用普通引号(`“`)替换引号(`”`)
【发布时间】: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', '"')出了什么问题

【问题讨论】:

    标签: python file encode


    【解决方案1】:

    a 是行的repr包含字符,但包含字符串\,u,@ 987654325@,0,1,d.

    因此将a = repr(line) 更改为a = line 将解决问题。

    【讨论】:

    • 非常感谢!
    猜你喜欢
    • 2011-01-26
    • 2018-09-23
    • 2011-02-06
    • 1970-01-01
    • 2021-08-12
    • 1970-01-01
    • 2021-02-07
    • 2015-07-30
    相关资源
    最近更新 更多