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 <ctc...@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@python.org
> http://mail.python.org/mailman/listinfo/tutor
>
>
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to