Python: Converting a string to an integer -
this question has answer here:
i need convert string file have integer. string in question 1 number.
l= linecache.getline('data.txt', 1) l=int(l) print l
i receive error:
valueerror: invalid literal int() base 10: '\xef\xbb\xbf3\n'
how convert string integer?
the file contains utf-8 bom.
>>> import codecs >>> codecs.bom_utf8 '\xef\xbb\xbf'
linecache.getline
not support encoding.
use codecs.open
:
with codecs.open('data.txt', encoding='utf-8-sig') f: l = next(f) l = int(l) print l
Comments
Post a Comment