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

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -