[Tutor] Fwd: Need help with reading and cleaning CSV files for a class

Alan Gauld alan.gauld at yahoo.co.uk
Sun Jan 28 09:35:51 EST 2018


On 28/01/18 13:36, Geoff Hancock wrote:
> Good day-

> I have been asked to help teach students how to clean up a CSV file in
> Python.

I'm not sure what you mean by "clean up" a CSV file.
If the file is malformed then turning it into a well
formed CSV file is a non trivial text processing task.

> Tomorrow I have to show students how to get a CSV file and write code to
> clean and parse it.

Parsing it is easy, and the python.org CSV module documentation
includes many examples, so I'd start there. (I'd also show your
students that page as a starter too, since learning to go to the
documentation is a good thing.)

Maybe structure the class around analyzing those examples
then trying some of your own?

The easiest way to generate a non trivial CSV file is probably
to save a spreadsheet from Excel...

> I understand the process--BUT I don't know how to do it!
> What I'm looking for is a simple
> Step 1
> Step 2
> Step 3 etc

Umm, I'm not sure what you need here.
A CSV file is opened as per a normal file - do you know how to do that?

Don't forget to include the DictReader too because it is often
better for extracting a sub set of the CSV fields.

If you are covering writing CSV files too - although thats
less common from Python, you also have the writer and
DictWriter classes, and although they do have examples
there isn't as much there.

Finally a fun thing todo in a classroom environment is to
get the students to buddy up and create a CSV file, then
pass it to their buddy and get them to decode it.

You could also try getting them to read a CSV file the hard
way using string handling methods alone, that way the advantages
of the CSV tools are more obvious to them!

It depends how much time you have. FOr a single study session
I'd go with reviewing the CSV module plus a "real world" example.

This assumes they even know what a CSV file is. If not, half
the lesson could be taken up on the file structure etc before
you even get to the code stuff.


-- 
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