Skip to main content
 首页 » 编程设计

Python 正则表达式 £ 到 Â char

2024年08月25日29mengfanrong

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

 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.