我需要将一个文件中的字符串转换为整数。这个字符串只包含一个数字。
L= linecache.getline('data.txt', 1)
L=int(L)
print L
我收到了这个错误信息:
ValueError: invalid literal for int() with base 10: '\xef\xbb\xbf3\n'
我该如何将这个字符串转换为整数?
>>> import codecs
>>> codecs.BOM_UTF8
'\xef\xbb\xbf'
linecache.getline
不支持编码。
请使用 codecs.open
替代:
with codecs.open('data.txt', encoding='utf-8-sig') as f:
L = next(f)
L = int(L)
print L