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

Rafael Durán Castañeda rafadurancastaneda at gmail.com
Thu Mar 24 15:51:34 CET 2011


I don't know what are you going to do with data, but you can save all data
in either a list or a dictionary, for example:

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='"')

diet_list = []
diet_dict = {}

for char,meal,ate,qty,com in reader:
   if char != 'Character':
      diet_list.append([char, meal, ate, qty, com])
      if diet_dict.has_key(char):
         diet_dict[char].append([meal, ate, qty, com])
      else:
         diet_dict[char] = [[meal, ate, qty, com],]

print diet_list
print diet_dict

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

> Thank you for your help
>
> Sent from my BlackBerry® on the MetroPCS Network
> ------------------------------
> *From: * Louis LEICHTNAM <louis.leichtnam at sgcib.com>
> *Date: *Thu, 24 Mar 2011 09:48:04 -0400
> *To: *<l.leichtnam at gmail.com>
> *Subject: *Re: Fw: [Tutor] Parsing data from a csv file [NC]
>
>
> Hello the loops are a bit different, and I definitely don't know if it's
> correct but I want to be able to put each part of the cvs into my future
> html code.
>
> the cvs file is this one:
>
>
> and can be found there *
> http://www.cs.columbia.edu/~joshua/teaching/cs3101/simpsons_diet.csv*<http://www.cs.columbia.edu/%7Ejoshua/teaching/cs3101/simpsons_diet.csv>
>
>
>
>  *l.leichtnam at gmail.com*
>
> 03/24/2011 09:42 AM
>  Please respond to
> l.leichtnam at gmail.com
>
>    To
> Louis LEICHTNAM/us/socgen at socgen
> cc
>   Subject
> Fw: [Tutor] Parsing data from a csv file
>
>
>
>
>
>
> On Thu, Mar 24, 2011 at 8:34 AM, louis leichtnam <*l.leichtnam at gmail.com*<l.leichtnam at gmail.com>>
> wrote:
> Hello,
>
> I am to extract a csv file from the web and parse it in python and after
> that make it into an html page.
>
> I want to get the data out of the csv file, get rid of the "," and make it
> readable.
>
> Questions:
> I would like to be able to print the line I want but I don't know how to do
> it.
> I would like to modify some lines, and make them cleaner
>
> Can you help me?
>
>
>
>
> 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='"')
> a=[]
> b=[]
> c=[]
> d=[]
> e=[]
> for char,meal,ate,qty,com in reader:
>
> at this point  you have the five fields you want
> if you do print char, meal, ate, qty,com you will see your data with the
> commas gone
>
>     for x in char:
>         a.append(x)
>     for y in meal:
>         b.append(y)
>     for z in ate:
>         c.append(z)
>     for aa in qty:
>         d.append(aa)
>     for bb in com:
>         e.append(bb)
> for i in range(1::):
>     print a[i]+b[i]+c[i]+d[i]+e[i]
>
>
> These for loops puzzle me.  It looks like you are iterating over each
> character in each variable, then creating a list containing each character.
> I don't think you want to do that.
>
> The for i in range(1::): will give you an error.  The range parameter needs
> to be something with a length, like a string, or a list, or a tuple.
>
> Can you show a little bit of your data file, and give us the traceback and
> output of your program?
> _______________________________________________
> 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>
>
>
>
>
> --
> Joel Goldstick
>
> *************************************************************************
> 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.
> *************************************************************************
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20110324/d9e02ba0/attachment.html>


More information about the Tutor mailing list