[Tutor] reading complex data types from text file
Christian Witts
cwitts at compuscan.co.za
Thu Jul 16 11:12:05 CEST 2009
Chris Castillo wrote:
> why does your 3rd and fourth lines have brackets?
>
> On Thu, Jul 16, 2009 at 1:08 AM, Christian Witts
> <cwitts at compuscan.co.za <mailto:cwitts at compuscan.co.za>> wrote:
>
> Chris Castillo wrote:
>
> I'm having some trouble reading multiple data types from a
> single text file.
>
> say I had a file with names and numbers:
>
> bob
> 100
> sue
> 250
> jim
> 300
>
> I have a few problems. I know how to convert the lines into an
> integer but I don't know how to iterate through all the lines
> and just get the integers and store them or iterate through
> the lines and just get the names and store them.
>
> please help.
> ------------------------------------------------------------------------
>
> _______________________________________________
> Tutor maillist - Tutor at python.org <mailto:Tutor at python.org>
> http://mail.python.org/mailman/listinfo/tutor
>
>
> You could do it with a list comprehension
>
> >>> names = []
> >>> numbers = []
> >>> [numbers.append(int(line.strip())) if line.strip().isdigit()
> else names.append(line.strip()) for line in open('test.txt','rb')
> if line.strip()]
> [None, None, None, None, None, None]
> >>> names, numbers
> (['bob', 'sue', 'jim'], [100, 250, 300])
>
> The list comprehension would unfold to
>
> for line in open('test.txt', 'rb'):
> if line.strip():
> if line.strip().isdigit():
> numbers.append(line.strip())
> else:
> names.append(line.strip())
>
> And from there you can do what you like with the lists.
>
> --
> Kind Regards,
> Christian Witts
>
>
>
>>> [numbers.append(int(line.strip())) if line.strip().isdigit() else
names.append(line.strip()) for line in open('test.txt','rb') if
line.strip()]
[None, None, None, None, None, None]
Are you referring to these lines ?
If so, the reason is that for Python to recognize it as a list
comprehension it needs to be wrapped in square brackets, if you were to
use () instead to wrap around it it would become a generator expression
(something which is incredibly powerful for larger amounts of data as it
iterates when it needs to instead of pre-building everything. And the
following line with the Nones on is because that is the output of the
calls to .append. Normally you wouldn't see it in your application though.
--
Kind Regards,
Christian Witts
More information about the Tutor
mailing list