[Tutor] Fw: Fw: Fw: Parsing data from a csv file [NC]

Rafael Durán Castañeda rafadurancastaneda at gmail.com
Thu Mar 24 20:25:09 CET 2011


Look again the code from my last message:

import urllib, csv
url=r"http://www.cs.columbia.edu/~joshua/teaching/cs3101/simpsons_diet.csv"
simpsons=urllib.urlopen(url)
reader=csv.reader(simpsons, delimiter=',',quotechar='"')

list_string = []

for char,meal,ate,qty,com in reader:
   if char != 'Character':
      list_string.append("%s %s %s %s %s" % (char, meal, ate, qty, com))

print list_string

Run it and look the output, you will see that the strings from list_string
are very close to the output you want. If you look specially this line:

list_string.append("%s %s %s %s %s" % (char, meal, ate, qty, com))

You will see you need just change the variable's order and add some words to
the string and you will get exactly the output you want, just try it!

2011/3/24 <l.leichtnam at gmail.com>

> Can you help me please?
>
> Sent from my BlackBerry® on the MetroPCS Network
> ------------------------------
> *From: * Louis LEICHTNAM <louis.leichtnam at sgcib.com>
> *Date: *Thu, 24 Mar 2011 11:51:26 -0400
> *To: *<l.leichtnam at gmail.com>
> *Subject: *Fw: [Tutor] Fw: Fw: Parsing data from a csv file [NC]
>
>
> ----- Forwarded by Louis LEICHTNAM/us/socgen on 03/24/2011 11:51 AM -----
>  *Louis LEICHTNAM/us/socgen*
>
> 03/24/2011 11:48 AM
>   To
> tutor-bounces+l.leichtnam=gmail.com at python.org, joel.goldstick at gmail.com,
> tutor at python.org, rafadurancastaneda at gmail.com
> cc
>   Subject
> Re: Fw: [Tutor] Fw: Fw: Parsing data from a csv file [NC]Link<Notes:///852577BC000C6413/DABA975B9FB113EB852564B5001283EA/60677866DC5D96E58525785D00560CBB>
>
>
>
> For example the line 1 in the csv file is:
>
> Barney Gumble, Breakfast, Duffbeer, 1, I could?ve gone to Harvard
>
> and I want to be able to clean it and turn it into:
>
> Barney Gumble had for beakfast 1 Duffbeer while thinking "I coul?ve gone to
> Harvard"
>
> so basically using in this line the column0 + "had for " + column 1 +
> column3 + column2 + "while thinking"+column4
>
> and I want to be able to do this for every line, except for line0.
>
> My end game is, after I'm able to do this, I want to write an Html using
> python that shows those cleand lines
>
> Thanks for your help
>
>
>
>
>  *l.leichtnam at gmail.com*
>
> 03/24/2011 11:39 AM
>  Please respond to
> l.leichtnam at gmail.com
>
>    To
> Louis LEICHTNAM/us/socgen at socgen
> cc
>   Subject
> Fw: [Tutor] Fw: Fw: Parsing data from a csv file [NC]
>
>
>
>
> Ok, I think what you are trying is to get all data in a string list:
>
> import urllib, csv
> url=r"*
> http://www.cs.columbia.edu/~joshua/teaching/cs3101/simpsons_diet.csv*<http://www.cs.columbia.edu/%7Ejoshua/teaching/cs3101/simpsons_diet.csv>
> "
> simpsons=urllib.urlopen(url)
> reader=csv.reader(simpsons,delimiter=',',quotechar='"')
>
> list_string = []
>
> for char,meal,ate,qty,com in reader:
>    if char != 'Character':
>       list_string.append("%s %s %s %s %s" % (char, meal, ate, qty, com))
>
> print list_string
>
> Doing something like this you can format all strings as you want and keep
> them into a list
>
> 2011/3/24 <*l.leichtnam at gmail.com* <l.leichtnam at gmail.com>>
> Thanks, what I'm trying to do is extract the data from the csv file, clean
> it to make each line into a sentence.
>
> But for this I think I need to use a dictionary for each and enter the
> words of each columns in one no?
> Sent from my BlackBerry® on the MetroPCS Network
>
> -----Original Message-----
> From: Rafael Durán Castañeda
>        <*rafadurancastaneda at gmail.com* <rafadurancastaneda at gmail.com>>
> Sender: tutor-bounces+l.leichtnam=*gmail.com* <http://gmail.com/>@*
> python.org* <http://python.org/>
> Date: Thu, 24 Mar 2011 15:51:34
> To: <*tutor at python.org* <tutor at python.org>>
> Subject: Re: [Tutor] Fw: Fw: Parsing data from a csv file [NC]
>
> _______________________________________________
> Tutor maillist  -  *Tutor at python.org* <Tutor at python.org>
> To unsubscribe or change subscription options:*
> **http://mail.python.org/mailman/listinfo/tutor*<http://mail.python.org/mailman/listinfo/tutor>
>
>
> *************************************************************************
> This message and any attachments (the "message") are confidential, intended
> solely for the addressee(s), and may contain legally privileged information.
> Any unauthorised use or dissemination is prohibited.  E-mails are susceptible
> to alteration.  Neither SOCIETE GENERALE nor any of its subsidiaries or
> affiliates shall be liable for the message if altered, changed or falsified.
> *************************************************************************
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110324/9ef056ca/attachment-0001.html>


More information about the Tutor mailing list