Newbie Question

Sion Arrowsmith siona at chiark.greenend.org.uk
Fri Aug 19 11:38:35 EDT 2005


Tom Strickland <strick_nine at comcast.net> wrote:
>I have a file that contains many lines, each of which consists of a string 
>of comma-separated variables, mostly floats but some strings. Each line 
>looks like an obvious tuple to me. How do I save each line of this file as a 
>tuple rather than a string? Or, is that the right way to go?

Depending on exactly what format you've got, either .split(',') on
the line, or if this is insufficient look at the csv module. You'll
then need some way of turning the list of strings both of these
will give you into the mixed float/string tuple you want, which
could be somewhat tedious. Without knowing what these lines look
like, or what they represent, I can't begin to guess how you might
go about it. Actually, I can -- I'd start by considering whether a
dict might be more appropriate than a tuple and use csv.DictReader.

-- 
\S -- siona at chiark.greenend.org.uk -- http://www.chaos.org.uk/~sion/
  ___  |  "Frankly I have no feelings towards penguins one way or the other"
  \X/  |    -- Arthur C. Clarke
   her nu becomeþ se bera eadward ofdun hlæddre heafdes bæce bump bump bump



More information about the Python-list mailing list