[Tutor] De-CSV-ing?

Curtis Larsen curtis.larsen@Covance.Com
Mon, 04 Jun 2001 16:10:36 -0500


What would be the easiest (or simplest) way to convert a line from a CSV
file into a list or tuple?  Sure, you can use ".split(',')" to break the
line into a list -- that's the easy part.  For example, if each read
line in a CSV file were represented by string "s":

s = "1,2,3,5.5,3.95,dog,cat,ferret"
s1 = s.split(",")
s1 now equals ['1', '2', '3', '5.5', '3.95', 'dog', 'cat', 'ferret']

That's great as far as it goes -- you have each value as a
uniquely-addressable item again.  But is there an easy way to convert
the numbers-as-strings field values back to being numbers?  Also, how
would you handle a CSV file where the strings were actually delimited by
(double or single) quotes?  (e.g. the record would look like:
1,2,3,"dog","cat","ferret",7.95 )  Do you have to loop through each
value in each line, or is there an easier/faster conversion approach?

Thanks!
Curtis



-----------------------------------------------------
Confidentiality Notice: This e-mail transmission 
may contain confidential or legally privileged 
information that is intended only for the individual 
or entity named in the e-mail address. If you are not 
the intended recipient, you are hereby notified that 
any disclosure, copying, distribution, or reliance 
upon the contents of this e-mail is strictly prohibited. 

If you have received this e-mail transmission in error, 
please reply to the sender, so that we can arrange 
for proper delivery, and then please delete the message 
from your inbox. Thank you.