python skipping lines?

Larry Bates larry.bates at websafe.com
Mon Nov 27 14:10:05 EST 2006


lisa.engblom at gmail.com wrote:
> thats easy enough to solve
> 
> """test text.py
> for playing around with my text editing task
> """
> 
> UnitList = open('/Python25/working/FacList.txt', 'r')
> RawData = open('/Python25/working/data.txt', 'r')
> Output = open('/Python25/working/output.txt', 'a')
> 
> def PullHourlyData(filename, facility, unit):
>         for line in filename:
>                 data = line.split('","')
>                 CurrentFacility = data[1]
>                 CurrentUnit = data[3]
>                 CurrentSO2Mass = data[9]            #in lb/hour
>                 #print CurrentFacility
>                 #print CurrentUnit
> 
>                 if facility == CurrentFacility and unit == CurrentUnit:
> 
>                         #print >> Output, '"%s", "%s", "%s"' %
> (CurrentFacility, CurrentUnit, CurrentSO2Mass)
>                         print '"%s", "%s", "%s"' % (CurrentFacility,
> CurrentUnit, CurrentSO2Mass)
>                 else:
>                         print facility
>                         print unit
>                         print CurrentFacility
>                         print CurrentUnit
>                         print "\n"
> 
> 
> counter = 0
> 
> for combos in UnitList:
>         print counter
>         FandU = combos.split('","')
>         #print combos
>         FacilityName = FandU[0]
>         UnitName = FandU[1]
>         #print FacilityName
>         #print UnitName
>         FacilityName = FacilityName.strip('"')
>         UnitName = UnitName.strip('",\n')
>         print FacilityName
>         print UnitName
> 
>         PullHourlyData(RawData, FacilityName, UnitName)
>         counter += 1
> 
> 
> UnitList.close()
> RawData.close()
> Output.close()
> print "Done!"
> 
> Jordan Greenberg wrote:
>> lisa.engblom at gmail.com wrote:
>>> Hi,
>> <SNIP>
>>> Any ideas of what could be the problem?
>>>
>> Hard to say without seeing your code.
>>
>> Jordan Greenberg
>>
>> -- 
>> Posted via a free Usenet account from http://www.teranews.com
> 

The first time through the loop you read through RawData and are at
the bottom of the file.  You either need to seek back to the beginning
or you need to close and reopen the file each time through the loop.

Suggestions:

1) In PullHourData the first argument is filename.  In fact
it is not a filename but rather a file pointer.  Just a little
confusing for anyone coming along behind you.

2) If the data is well-formed CSV you should probably take a look
at the csv module.  It handles CSV data better than splitting on
commas (which can be dangerous as there can be commas inside of
literal data).

-Larry



More information about the Python-list mailing list