Writing to .csv file errors - STJRush/handycode GitHub Wiki
Spaces between each line when writing to a csv file
eg.
`1,2,3`
`4,3,4`
`4,2,3`
instead of
`1,2,3`
`4,3,4`
`4,2,3`
This problem is caused by a problem in this line:
f = open(path, "w", newline = '')
You are most likely missing newline = ''
IndexError: list index out of range
This happens because python is trying to edit a column 3 of your .csv file when you file only has a column 1 and 2.
OR
This happens because your .csv file is empty or missing a column that was supposed to be there.
Either way, you need to open your .csv file and check how many columns there are. Remember to start counting at zero. The first column is column 0, the second is column 1, the third is 2 etc.
FileNotFoundError: [Errno 2] No such file or directory: 'whatever.csv'
It's exactly as it says. Either
-
You forgot to put your python code (.py file) into the same folder as the .csv file.
-
You have changed the name of your .csv file and have forgotten to update the code replacing the old with the new file name.
ValueError: invalid literal for int() with base 10: ''
Your .csv file has a space in the column and python expected everything to be integers. The space is represented by ''
You need to remove the space from the .csv file.
If there is a space in all columns on that row, you can tell python to skip that row by as follows:
After the line: reader = csv.reader(f)
add this: header = next(reader)
ValueError: invalid literal for int() with base 10: 'SOME WORD'
Your .csv file has a WORD or HEADING in the column and python expected everything to be integers. It doesn't like 'SOME WORD'.
You need to remove the word from the .csv file or you can tell python to skip that row by as follows:
After the line: reader = csv.reader(f)
add this: header = next(reader)
ValueError: invalid literal for int() with base 10: '1.2' (or some decimal)
Your .csv file has a decimal in the column and python expected everything to be integers (eg. 1,2,3) without any decimal point. It doesn't like floats (eg. 1.0, 2.0, 3.0).
You need to remove the decimal OR better, change the int() on the line causing the error to float()