Python 正则表达式 £ 到 char

标签 python regex python-3.x

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

 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 时,我在 notepad++ 中看到了类似的问题,对于 £纬度£)。显示相同行为的示例 -

我的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/

相关文章:

Java RegEx 不会替换所有

java - 将一串数字拆分为单独的数字

python - 在 ZOBD OOBTree 中使用对象作为键的正确方法是什么?

python - 如何在 Pygame 中将 'blit' 图像显示到屏幕上

python - TensorFlow 中的默认 tf.gradients - 全导数还是偏导数?

Python Django Errno 54 'Connection reset by peer'

c# - 正则表达式匹配IP地址+通配符

python - 打印整数或带 n 位小数的 float

python-3.x - 带有美元符号的 Pandas 到 MatPlotLib

Python 3.6 安装 win32api?