python - python 正则表达式的 UnicodeDecodeError

标签 python unicode

我正在尝试用空格替换所有制表符,以便我可以将逗号分隔的文本放在另一个文件的一行中。现在我的代码如下所示:

from __future__ import print_function
import re
import ast

f = open('sample_test.txt', 'r')
g = open('sample_test1.txt', 'w')

for line in f:
        c = re.sub(r'\R', r' ', line.rstrip())
        print (c, file = g)
f.close()

现在的问题是我收到此错误:

UnicodeDecodeError: 'charmap' codec can't decode byte 0x98 in position 1944: character maps to <undefined>

最佳答案

utf-8 格式打开文件,如果您只想替换制表符,也不需要正则表达式:

import io

with io.open('sample_test.txt', encoding="utf-8") as f, io.open('sample_test1.txt', 'w', encoding="utf-8") as g: 
    for line in f:
        g.write(line.replace("\t"," "))

关于python - python 正则表达式的 UnicodeDecodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37510616/

相关文章:

python - 类型错误 : object of type 'map' has no len()

python - 重写 __new__ 的基元类生成带有错误 __module__ 的类

java - Unicode 日语长音标记从假名脚本中排除?

python - json序列化输出重音字符不正确,python/django

python - 存储和重新加载 matplotlib.pyplot 对象

python - 如何删除 pytest-html 报告中的环境表

Python 正则表达式处理多个换行符

php - 如何在 PHP 中匹配除 "-"以外的所有特殊字符的正则表达式?

python - 更改表时出现 UnicodeDecodeError

python - 如何从任何非 unicode\特殊字符、html 标记、js 中清除字符串 - 保留纯文本和标点符号 - 在 python 中?