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

2018-01-29 Thread Neil Cerutti
On 2018-01-28, Geoff Hancock  wrote:
> Good day-
> I'm in a difficult situation.
> I have been asked to help teach students how to clean up a CSV
> file in Python.

Can you give an example of the kind of thing you need to teach?

It is malformed csv that has to be changed into well-formed?

That's sounds like a fun and challenging problem.

-- 
Neil Cerutti

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-01-28 Thread Alan Gauld via Tutor
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


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-01-28 Thread leam hall
On Sun, Jan 28, 2018 at 8:36 AM, Geoff Hancock  wrote:
> Good day-
> I'm in a difficult situation.
> I have been asked to help teach students how to clean up a CSV file in
> Python.
> The job has fallen to me because the teacher of the course is out on
> emergency leave and I just found out.
> Tomorrow I have to show students how to get a CSV file and write code to
> clean and parse it.
>
> 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
> I'm running the latest version of Python and am running on Windows
> Can anyone assist?
>
> Much appreciate the time
> JGH


Geoff, I'm not that great a coder but here's where I would start.
Assume a file name "gang_draft.csv" that is colon delimited since I'm
working on stuff for an RPG session.  :)



file = open('gang_draft.csv', 'r')
for line in file:
  line = line.strip()
  line_array = line.split(':')
  print(line_array[1])



Where I have print you can do whatever cleanup you like. Then either
print specific columns or join them back into a string.

Hope that helps start you in the right direction. Others here will
likely come up with better solutions.

Leam
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


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

2018-01-28 Thread Geoff Hancock
Good day-
I'm in a difficult situation.
I have been asked to help teach students how to clean up a CSV file in
Python.
The job has fallen to me because the teacher of the course is out on
emergency leave and I just found out.
Tomorrow I have to show students how to get a CSV file and write code to
clean and parse it.

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
I'm running the latest version of Python and am running on Windows
Can anyone assist?

Much appreciate the time
JGH
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor