473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

python skipping lines?

Hi,

I've just started programming in python, and have run into an
unexpected problem. I am using python to pull text data from some csv
files. I have one file that has the important identifiers (name, etc)
and other files with lots of other data. I wrote a function that takes
the file name and identifiers as inputs, selects the data I need, and
outputs it to another file. This function (PullData) seems to work
fine.

In the same .py file, but separate from the function, I entered used
"for line in file" to step through each line in my identifier file and
feed the identifiers into my PullData function. It works fine for the
first entry in my identifier file, but it will not call the function
again. It will execute lines before or after the appropriate number of
times given the number of lines in the identifier file. i just put in
a dummy counter variable to try to figure out why it wasn't working)
.... but it always skips the function call after the first line.

Any ideas of what could be the problem?

Nov 27 '06 #1
6 3107
li**********@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

Nov 27 '06 #2
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:
li**********@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
Nov 27 '06 #3
li**********@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:
>li**********@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
Nov 27 '06 #4
li**********@gmail.com wrote:
RawData = open('/Python25/working/data.txt', 'r')
You open this file only once. The first time in here:
def PullHourlyData(filename, facility, unit):
for line in filename:
reads all of the file - nothing left for the other function calls!

A better way would be to read the file in once to a list and give that
lkist to the function to process.

HTH,
Jussi
Nov 27 '06 #5
I'm not sure if this will /solve/ your problem, but it's
something I noticed...
UnitList = open('/Python25/working/FacList.txt', 'r')
RawData = open('/Python25/working/data.txt', 'r')
Here, you open RawData once...
Output = open('/Python25/working/output.txt', 'a')

def PullHourlyData(filename, facility, unit):
for line in filename:
[cut]
counter = 0

for combos in UnitList:
[cut]
PullHourlyData(RawData, FacilityName, UnitName)
counter += 1
and your first pass through this loop, you exhaust RawData by
reading to the end. The second pass through the UnitList loop,
you pass the exhausted RawData to PullHourlyData(). I'm thinking
you'd need to RawData.seek(0) or some such "rewind" ability. A
slightly ugly alternative would be just opening/closing the
RawData file for each pass through the loop. Another, possibly
cleaner option (depending on the size of RawData's contents)
would be to just read the whole thing into an in-memory list with
something like

data = RawData.readlines()

and then just pass "data" to PullHourlyData() each time...no need
to rewind (like VHS vs. DVD :)

Part of the confusion stems from the fact that what you refer to
as "filename" (sounds like it should be a string containing the
path to the file) is actually a file object that contains state.

-tkc


Nov 27 '06 #6
Thanks, everyone for replying so promptly. I got it to work the way I
intended, and have some ideas for how to make it much cleaner.

- Lisa

Nov 27 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
0
by: Andrew Ayre | last post by:
Hi, I can't seem to get the library built, and any help is greatly appreciated. Here is the info: Windows XP Borland C++ Builder 5 Latest Boost source code (downloaded at the weekend) Windows...
5
by: Michael Sperlle | last post by:
Is it possible? Bestcrypt can supposedly be set up on linux, but it seems to need changes to the kernel before it can be installed, and I have no intention of going through whatever hell that would...
7
by: Gustaf | last post by:
Hi all, Just for fun, I'm working on a script to count the number of lines in source files. Some lines are auto-generated (by the IDE) and shouldn't be counted. The auto-generated part of files...
0
by: Jerry Coffin | last post by:
In article <4fae62b0-6858-4e9e-830e-9eecf6691d4a@ 59g2000hsb.googlegroups.com>, friend.blah@googlemail.com says... Each time you read from the file, keep track of the file position after...
4
by: BibI | last post by:
Hi there, I just started programming with PERL and am trying to put together my first little data manipulation program. I am working on a MAC with OSX. I have a data file with the following...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.