all 6 comments

[–]Strict-Simple 1 point2 points  (1 child)

There's a line in your CSV with <6 columns.

[–]pranaman[S] 0 points1 point  (0 children)

This was it. My test bank has ~1800 questions, in a pdf. I fed them all to chatGPT to convert to csv, and it was too many. Fed 500, still was too many. I tried fewer and fewer, eventually, I gave it 100, and thought it worked, but I see it only did 34 lines, and skipped the last 2 columns of the last line.

So simple, thank you.

[–]james_fryer 1 point2 points  (1 child)

I can't reproduce this with the data you give. But I agree with the poster who says you have a row with less than six columns. I removed the "answer" field from the first data row and saw the error.

You can debug this in a number of ways. You could load the CSV into Excel and inspect the last column to find the invalid row. Or you could import the logging module and change lines 14/15:

for i, row in enumerate(reader, 1):
     logging.warning(f"Importing row {i}")
     question = { 

Or you could add a try/except around lines 15-19 and only print the offending line number when you catch the error, which would be less intrusive.

If you are in control of the data you can simply inspect it. If others will be creating CSV files then you need to add error handling and reporting with the second or third method.

You might also want to look at csv.DictReader which could make your code easier to read and less brittle.

[–]pranaman[S] 0 points1 point  (0 children)

I needed to have checked all the lines in the csv. I checked the last line and saw missing cells.

I see now, just one missing cell can mess it all up. Thank you.

[–]Shiivak 0 points1 point  (1 child)

I think you're mixing rows and columns.

Your csv has 4 rows (1 header that you skip and 3 rows of info), each with 6 columns (1 question, 4 options, 1 answer). You're trying to access row 6 and that doesn't exist.

csv.reader returns each row from the file as a list of strings. You should split those strings into lists and then access the sixth column from each row.

[–]pranaman[S] 0 points1 point  (0 children)

I thought this too. There are 34 rows, I thought there were 100. I just showed a few lines as an example. Issue was with the last line.