1

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

  • Does this answer your question? [Python read csv - BOM embedded into the first key](https://stackoverflow.com/questions/40310042/python-read-csv-bom-embedded-into-the-first-key) – BallpointBen May 02 '20 at 16:51
  • Nope. i still got ValueError: invalid literal for int() with base 10: 893245\n332118\n9334343236\n565480\n78996\n889015\n5056515\n378677914\ – Arthur Morgan May 02 '20 at 16:56
  • 1
    You have to loop over the lines in your CSV before splitting on commas. Any reason you aren't using the `csv` module? – BallpointBen May 02 '20 at 16:58

1 Answers1

1
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.

Bangbangbang
  • 560
  • 2
  • 12