[英]Korean txt file encoding with utf-8
我正在尝试使用python处理韩国文字文件,但是当我尝试使用utf-8编码文件时却失败了。
#!/usr/bin/python
#-*- coding: utf-8 -*-
f = open('tag.txt', 'r', encoding='utf=8')
s = f.readlines()
z = open('tagresult.txt', 'w')
y = z.write(s)
z.close
============================================================= Traceback (most recent call last): File "C:\\Users\\******\\Desktop\\tagging.py", line 5, in <module> f = open('tag.txt', 'r', encoding='utf=8') TypeError: 'encoding' is an invalid keyword argument for this function [Finished in 0.1s] ==================================================================
当我刚打开一个用utf-8编码的韩文txt文件时,字体就这样折断了。 我能做什么?
\\ xc1 \\ xc1 \\ xbe \\ xc6 \\ xc1 \\ xf6 \\ xb4 \\ xc2 \\ n','\\ xc1 \\ xc1 \\ xbe \\ xc6 \\ xc7 \\ xcf \\ xb0 \\ xc5 \\ xb5 \\ xe7 \\ xbf \\ xe4 \\ n',' \\ xc1 \\ xc1 \\ xbe \\ xc6 \\ xc7 \\ xcf \\ xbd \\ xc3 \\ xb4 \\ xc2 \\ n','\\ xc1 \\ xcb \\ xbc \\ xdb \\ xc7 \\ xd1 \\ xb5 \\ xa5 \\ xbf \\ xe4 \\ n',' \\ xc1 \\ xd6 \\ xb1 \\ xb8 \\ xbf \\ xe4 \\
我不懂韩语,也没有示例字符串可以尝试,但是这里有一些建议:
1个
f = open('tag.txt', 'r', encoding='utf=8')
您在这里输入错字, utf-8
而不是utf=8
,这解释了您遇到的异常。
open()
的默认模式是“ r”,因此您无需再次定义它。
2不要只使用open
,还应该使用context manager语句来管理打开/关闭文件描述符,如下所示:
with open('tagresult.txt', 'w') as f:
f.write(s)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.