[Tutor] reading complex data types from text file

Glen Zangirolami digitalman66 at gmail.com
Thu Jul 16 15:26:53 CEST 2009


All lines that come back from a text file come back as strings. You can use
string methods to detect the data like so:
f = open('test.txt')
lines = f.readlines()
numbers = []
strings = []

for line in lines:
    if line.strip().isdigit():
        numbers.append(int(line))
    else:
        strings.append(line.strip())

print numbers
print strings



On Wed, Jul 15, 2009 at 1:55 PM, Chris Castillo <ctcast at gmail.com> 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
> http://mail.python.org/mailman/listinfo/tutor
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20090716/2816d2f8/attachment-0001.htm>


More information about the Tutor mailing list