I need to convert a string from a file that I have into an integer. The string in question is just one number.
L= linecache.getline('data.txt', 1)
L=int(L)  
print L   
I receive the error:
ValueError: invalid literal for int() with base 10: '\xef\xbb\xbf3\n'
How do I convert this string into an integer?
The file contains an UTF-8 BOM.
>>> import codecs
>>> codecs.BOM_UTF8
'\xef\xbb\xbf'
linecache.getline does not support encoding.
Use codecs.open:
with codecs.open('data.txt', encoding='utf-8-sig') as f:
    L = next(f)
    L = int(L)
    print L   
                        Your file starts with a BOM. Strip it before trying to parse the number.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With