【发布时间】:2016-04-27 16:05:46
【问题描述】:
我正在尝试读取包含韩文和英文的 txt 文件。
这是一个示例:
52:15 问候。 안녕하십니까.
我的代码是:
# Read a line and Split into tokens
f = open(infile, 'r')
for line in f:
if( matchObj = re.match( r"(\d\d:\d\d)\t([^\t]+)\t(.*)$", line )
startTC, englishSubtitle, foreignSubtitle = matchObj.group(1), matchObj.group(2), matchObj.group(3)
else:
SyntaxError(line)
当我在运行 El Capitan 的 2012 Macbook Pro 上将它读入 python (3.5) 时,我收到错误消息(在底部)。
错误信息:
python3 *.py
Traceback (most recent call last):
File "txtToSrt.py", line 48, in <module>
readFileData( "Korean.txt" )
File "txtToSrt.py", line 26, in readFileData
for line in f:
File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/codecs.py", line 321, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 0: invalid start byte
您能否建议如何阅读此内容。
【问题讨论】:
-
0xfe- 可以是BOM
标签: python text fonts character-encoding non-ascii-characters