几天前,我在 Python 小程序中遇到了文件编码这个烦人的问题。
我经常使用 MediaWiki - 最近我将文档从 .doc 转换为 Wikisource。
Microsoft Word 格式的文档在 Libre Office 中打开,然后导出为 Wikisource 格式的 .txt 文件。我的程序正在搜索 [[Image:]] 标签并将其替换为从列表中获取的图像名称 - 该机制非常有效(非常感谢 brjaga 的帮助!)。 当我对自己创建的 .txt 文件进行一些测试时,一切正常,但是当我将 .txt 文件与 Wikisource 放在一起时,整个事情就不再那么有趣了:D
我在 Python 中收到这条消息:
Traceback (most recent call last):
File "C:\Python33\final.py", line 15, in <module>
s = ' '.join([line.replace('\n', '') for line in myfile.readlines()])
File "C:\Python33\lib\encodings\cp1250.py", line 23, in decode
return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x81 in position 7389: character maps to <undefined>
这是我的 Python 代码:
li = [
"[[Image:124_BPP_PL_PL_Page_03_Image_0001.jpg]]",
"[[Image:124_BPP_PL_PL_Page_03_Image_0002.jpg]]",
"[[Image:124_BPP_PL_PL_Page_03_Image_0003.jpg]]",
"[[Image:124_BPP_PL_PL_Page_03_Image_0004.jpg]]",
"[[Image:124_BPP_PL_PL_Page_03_Image_0005.jpg]]",
"[[Image:124_BPP_PL_PL_Page_03_Image_0006.jpg]]",
"[[Image:124_BPP_PL_PL_Page_03_Image_0007.jpg]]",
"[[Image:124_BPP_PL_PL_Page_05_Image_0001.jpg]]",
"[[Image:124_BPP_PL_PL_Page_05_Image_0002.jpg]]"
]
with open ("C:\\124_BPP_PL_PL.txt") as myfile:
s = ' '.join([line.replace('\n', '') for line in myfile.readlines()])
dest = open('C:\\124_BPP_PL_PL_processed.txt', 'w')
for item in li:
s = s.replace("[[Image:]]", item, 1)
dest.write(s)
dest.close()
好的,所以我做了一些研究,发现这是编码的问题。所以我安装了一个程序 Notepad++ 并用 Wikisource 将我的 .txt 文件的编码更改为:UTF-8 并保存。然后我对我的代码做了一些更改:
with open ("C:\\124_BPP_PL_PL.txt", encoding="utf8') as myfile:
s = ' '.join([line.replace('\n', '') for line in myfile.readlines()])
但是我收到了这个新的错误信息:
Traceback (most recent call last):
File "C:\Python33\final.py", line 22, in <module>
dest.write(s)
File "C:\Python33\lib\encodings\cp1250.py", line 19, in encode
return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\ufeff' in position 0: character maps to <undefined>
我真的被这个问题困住了。我想,当我在 Notepad++ 中手动更改编码然后我会告诉我设置的编码 - 一切都会很好。
请帮忙,提前谢谢。
请您参考如下方法:
当 Python 3 打开文本文件时,它会在尝试解码文件时使用系统的默认编码,以便为您提供完整的 Unicode 文本(str 类型完全支持 Unicode)。在写出此类 Unicode 文本值时,它的作用相同。
你已经解决了输入端;您在阅读时指定了编码。 写入时也这样做:指定一个编解码器用于写出可以处理 Unicode 的文件,包括代码点 U+FEFF 处的不间断空白字符。 UTF-8 通常是一个不错的默认选择:
dest = open('C:\\124_BPP_PL_PL_processed.txt', 'w', encoding='utf8')
您也可以在编写时使用 with 语句并节省 .close() 调用:
for item in li:
s = s.replace("[[Image:]]", item, 1)
with open('C:\\124_BPP_PL_PL_processed.txt', 'w', encoding='utf8') as dest:
dest.write(s)
