Search code examples
python-3.xopencsv

How to fix open csv error: invalid literal for int() with base


I am trying to read a CSV file but have the error below ValueError: invalid literal for int() with base 10: '893245\n332118\n9334343236\n565480\n78996\n889015\n5056515\n378677914

r = open('my_csv_file.csv')
data = [int(i) for i in r.read().split(',')]
print(data)

Can anyone tell me what's wrong with the above code? I am using Windows 10.

After I used r = open('my_csv_file.csv',,encoding='utf-8-sig') I still have the ValueError: invalid literal for int() with base 10: '893245\n332118\n9334343236\n565480\n78996\n889015\n5056515\n378677914


Solution

  • r = open('my_csv_file.csv',encoding='utf-8-sig')
    [int(i) for i in r.read().split('\n') if len(x)>0]
    

    The above code will do the trick.