[Tutor] Using if statement with csv file

Alan Gauld alan.gauld at btinternet.com
Wed Jan 28 11:30:07 CET 2015


On 27/01/15 22:40, dw wrote:

> I have ever read a small csv file into one variable.
> Even a csv file with 100 rows.
> And then split it into identified fields from there.
> I look at the csv file with Notepad++ to see if the data rows end with a
> \r or a \n.
> I then split that into a list and each row is an element in the list.
> I can then split each row by the comma delimiter, to resolve down to
> each field.
> Just an idea.

Probably not a good one though. The CSV module is a better approach 
since it can take account of all the complexities of CSV files which 
your approach would fail on.
For example how do you handle an embedded text field with commas in it? 
Or newlines? Or a quoted field containing quotes?

When a module exists in the standard library its nearly always worth 
using it. Somebody has taken the time to write it, test it and submit it 
precisely because the simple approach doesn't work reliably.

hth
-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos




More information about the Tutor mailing list