gpt4 book ai didi

Python 正则表达式 £ 到 Â char

转载 作者:太空宇宙 更新时间:2023-11-04 01:03:07 26 4
gpt4 key购买 nike

我正在编写一个搜索文件的程序,寻找 £ 符号:

 r = re.compile(r"£\S*£")
def parseData(self):
f = open(file, 'r')
fs = f.read()
res = r.findall(fs)
return res

出于某种原因,我的输出有 Â 个符号,例如 £foo£,其中文件为 £foo£。

如果有帮助,我正在使用 python 3.4.3。

完整文件读取 http://pastebin.com/L7hjeg6A

最佳答案

问题是文件以一种格式编码,但您以另一种格式打开文件。最有可能的是,该文件是 utf-8 ,但是您正在以某种 ANSI 格式打开(我在记事本++ 中看到了类似的问题,当我将编码从 UTF-8 更改为 ANSI 时,对于 £Latitude£)。显示相同行为的示例 -

我的a.txt -

£Latitude£

代码-

>>> f = open('a.txt','r')
>>> s = f.read()
>>> s
'\xc2£Latitude\xc2£'

>>> f = open('a.txt','r',encoding='utf-8')
>>> s = f.read()
>>> s
'£Latitude£'

您需要以正确的编码打开文件,方法是将编码作为参数传递给 open() ,就像上面所做的那样。


来自 documentation of open() -

encoding is the name of the encoding used to decode or encode the file. This should only be used in text mode. The default encoding is platform dependent (whatever locale.getpreferredencoding() returns), but any text encoding supported by Python can be used. See the codecs module for the list of supported encodings.

关于Python 正则表达式 £ 到 Â char,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31937272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com