Newbie Question

T

Tom Strickland

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?

Thank you.

Tom Strickland
 
S

Sion Arrowsmith

Tom Strickland said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
474,263
Messages
2,571,312
Members
47,988
Latest member
HarryBeck

Latest Threads

Top