[Tutor] reading complex data types from text file
Christian Witts
cwitts at compuscan.co.za
Thu Jul 16 11:51:29 CEST 2009
Chris Castillo wrote:
> Oh okay. gotcha.
> so I have what I want basically. I just need to check to see if each
> number meets a certain criteria and output something like the
> following to a text file. Should I be going about this a different way
> or should I still use lists?
>
> bob below average
> sue above average
> jim perfect
>
>
>
> On Thu, Jul 16, 2009 at 4:12 AM, Christian Witts
> <cwitts at compuscan.co.za <mailto:cwitts at compuscan.co.za>> wrote:
>
> 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>
> <mailto: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> <mailto: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
>
>
>
Ok, I see what you want to do now. The best case for this would be to
unfold it into a proper loop instead to maintain readability
Name = None
Number = None
fOut = open('output.txt', 'wb')
for line in open('test.txt', 'rb'):
line = line.strip()
if line:
if line.isdigit():
Number = int(line)
# Do whatever processing you want now
else:
Name = line
if Name and Number: # Checks to see if Name, Number have values
fOut.write('%s\t%s\r\n' % (Name, Number))
Name, Number = None, None
# This is done so that you have to have a name and number
# before writing to the file, it also then resets the
# state of Name, Number to None before continuing
fOut.close()
What this will do is read in the file a line at a time, if the line is
empty is continues to the next one without processing. If the line
contains data it will check if it's numeric and if so populate the
Number variable and if non-numeric populate the Name variable (gross
assumption that it would indeed be a name and not something arbitrary
like punctuation etc). Once both variables have been set that data
would be written out to file for storing.
--
Kind Regards,
Christian Witts
More information about the Tutor
mailing list