0

Please help, I can't find it.

Why can I append row but not row[2]? It crashes. I'm using Python 3.4.3.

import csv

with open("file.csv", encoding="UTF8") as csvfile:
    read = csv.reader(csvfile, delimiter=";")

    original = []

    for row in read:
        original.append(row[2])


csvfile.close()
print(original)

Thanks

12
  • 3
    Perhaps row contains fewer than three elements? Commented Sep 3, 2015 at 18:09
  • 3
    Kevin is probably on to something. It's hard to debug a crash without seeing the error message from the crash. Commented Sep 3, 2015 at 18:10
  • 2
    How can you tell it crashed? :) Commented Sep 3, 2015 at 18:12
  • 2
    what is the error traceback from the crash? Commented Sep 3, 2015 at 18:14
  • 1
    Btw, just a note. There is no need in closing the file explicitly - it would be closed by the context manager - remove the csvfile.close() line. Commented Sep 3, 2015 at 18:15

2 Answers 2

1

This looks to be a frustrating debugging experience.

One possibility is that there's a last line in the file that has only one item which is causing the issue.

A quick way to look at the situation (depending how long your file is) might be to throw in a print and see what's going on, line by line:

for row in read:
    try:
        original.append(row[2])
    except:
        print(row)

If you run with this, you may be able to see what happens just before the crash.

You may want to be a little more descriptive on what the crash is. It's famously difficult to help with such a vague description. A little more effort will help people to help you more effectively.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. it freezes, it takes about 60% of CPU. Tried what you wrote and the problem thing is the print(original) part, the appending seems to work just fine. Maybe the list is to long? About 1400 items. But works if i append just row though.
I adjusted the code. It should now only print when it fails. Freezing does suggest a memory leak somewhere...
1

I would suggest you do not try and print the whole of your CSV list at the end, this can cause some IDEs to lock up for a long time.

Instead you could just print the last few entries to prove it has worked:

print("Rows read:", len(original)
print(original[-10:])

1 Comment

Yes, that's it. It was just too much at once ( 1408 ) Thank you! :-)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.