472,358 Members | 2,070 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,358 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 3001
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...
2
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and efficiency. While initially associated with cryptocurrencies...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it so the python app could use a http request to get...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and credentials and received a successful connection...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand. Background colors can be used to highlight important...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
0
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...

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.