R
29

I finally got Python to read my messy CSV file

I spent a whole afternoon fighting with a CSV export from a client's old system, kept getting errors because of stray quotes and commas inside the data. Then I found the csv module's quotechar parameter and set it to the right character, and it just worked on the first try. The fix was literally one line of code, but I'd been avoiding reading the docs for weeks. Has anyone else had a stubborn file format that turned out to have a simple built-in solution?
2 comments

Log in to join the discussion

Log In
2 Comments
emmasmith
emmasmith8h agoProlific Poster
Oh man, that feeling when the fix was right there all along is the worst and best at once...
5
rowan593
rowan5931d ago
And once you crack one format, you start seeing the pattern everywhere. Like I had the same thing with a pipe-delimited file, took me an hour to realize I just had to set delimiter='|' and boom, done. Or those old Excel exports that use semicolons depending on the region settings. The csv module handles all of it, you just gotta know which knob to turn. It's funny how we'll bang our heads against something for weeks when the answer is literally in the docs, just hidden behind a boring parameter name. Once you get comfortable poking around in that reference page, you save yourself so many headaches.
2