473,561 Members | 3,128 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

File Parsing Question

Hi,
I am new to Python. I am trying to do the following

inp = open(my_file,'r ')

for line in inp:
# Perform some operations with line
if condition something:
# Start re reading for that position again
for line in inp:
if some other condition
break
# I need to go back one line and use that line value.
# I need to perform the operations which are listed in the
top with this line
# value. I cannot push that operation here
# I cannot do this with seek or tell.

In Perl this is what I have
while (<inp){
# my_operations
next if /pattern/
while (<inp>) {
operations again
last if /pattern2/
}
seek(inp,(-1-length),1)
}

This works perfectly in Perl. Can I do the same in Python.

Thanks
Jee
Sep 12 '07 #1
7 1633

Save the previous line in a variable if you want the previous line
only.
for line in inp:
# Perform some operations with line
if condition something:
print prev_line
print line
break
# I need to go back one line and use that line value
--prev_line = line

If you want to do more than that, then use data=inp.readli nes() or you
can use
data = open(myfile), "r").readlines( ). The data will be stored in
list format so you can access each line individually.

Sep 12 '07 #2
I'm assuming you know that python has a file.seek(), but you have to
know the number of bytes you want to move from the beginning of the
file or from the current location. You could save the length of the
previous record, and use file seek to backup and then move forward,
but it is simpler to save the previous rec or use readlines() if the
file will fit into a reasonable amount of memory.

Sep 12 '07 #3
I would prefer to use something with seek. I am not able to use seek()
with "for line in inp". Use tell and seek does not seem to do anything
with the code. When I try to do

for line in inp.readlines() :
# Top of Loop
if not condition in line:
do_something
else:
for lines in inp.readlines() :
if not condition
do_something
else:
break
pos = inp.tell()
inp.seek(pos) ---This line has not effect in the program

Not sure if Iam missing something very basic. Also the previous line
needs to be used in the position I call # Top of Loop.

Thanks
On 9/12/07, Zentrader <ze********@gma il.comwrote:
I'm assuming you know that python has a file.seek(), but you have to
know the number of bytes you want to move from the beginning of the
file or from the current location. You could save the length of the
previous record, and use file seek to backup and then move forward,
but it is simpler to save the previous rec or use readlines() if the
file will fit into a reasonable amount of memory.

--
http://mail.python.org/mailman/listinfo/python-list
Sep 12 '07 #4
for line in inp.readlines() :

If you are now using readlines() instead of readline(), then
a) it is only used once to read all data into a container
b) you can access each element/line by it's relative number

data=open(filen ame, "r").readlines( )
for eachline in data : (not readlines())

so try
print data[0] ## first rec
print data[9] ## 10th rec, etc

you can use
ctr = 0
for eachline in data:
##do something
if ctr 0:
print "this line is", eachline ## or data[ctr]
print "prev_line = ", data[ctr-1]
ctr += 1

or a slightly different way
stop = len(data)
ctr=0
while ctr < stop:
## do something
if ctr 0 :
this_line = data[ctr]
prev_line = data[ctr-1]
ctr += 1

Sorry, I don't use file.seek() so can't help there

Sep 13 '07 #5
Am Wed, 12 Sep 2007 17:28:08 -0500 schrieb Shankarjee Krishnamoorthi:
I would prefer to use something with seek.
Writing Perl in any language?
I am not able to use seek()
with "for line in inp". Use tell and seek does not seem to do anything
with the code. When I try to do

for line in inp.readlines() :
readlines() reads the whole file at once, so inp.tell() will give the
position at the end of the file from now on.
# Top of Loop
if not condition in line:
do_something
else:
for lines in inp.readlines() :
if not condition
do_something
else:
break
pos = inp.tell()
inp.seek(pos) ---This line has not effect in the program

Not sure if Iam missing something very basic. Also the previous line
needs to be used in the position I call # Top of Loop.
If you want to use seek/tell you can't iterate over the file directly
because

for line in inp:
# ...

reads ahead to make that iteration highly efficient -- so you will often
get a position further ahead than the end of the current line.

But you can use readline() (which doesn't read ahead) in conjunction with
tell/seek; just replace all occurences of

for line in inp:
# ...

with

for line in iter(inp.readli ne, ""):
# ...

Peter
Sep 13 '07 #6
Dennis Lee Bieber wrote:
for line in inp:

will read one line at a time (I'm fairly sure the iterator doesn't
attempt to buffer multiple lines behind the scenes)
You are wrong:
>>open("tmp.txt ", "w").writelines ("%s\n" % (9*c) for c in "ABCDE")
instream = open("tmp.txt")
for line in instream:
.... print instream.tell() , line.strip()
....
50 AAAAAAAAA
50 BBBBBBBBB
50 CCCCCCCCC
50 DDDDDDDDD
50 EEEEEEEEE
>>>
Here's the workaround:
>>instream = open("tmp.txt")
for line in iter(instream.r eadline, ""):
.... print instream.tell() , line.strip()
....
10 AAAAAAAAA
20 BBBBBBBBB
30 CCCCCCCCC
40 DDDDDDDDD
50 EEEEEEEEE
>>>
Peter
Sep 13 '07 #7
Great. That worked for me. I had some of my routines implemented in
Perl earlier. Now that I started using Python I am trying to do all my
automation scripts with Python. Thanks a ton

Jee

On 9/13/07, Peter Otten <__*******@web. dewrote:
Dennis Lee Bieber wrote:
for line in inp:

will read one line at a time (I'm fairly sure the iterator doesn't
attempt to buffer multiple lines behind the scenes)

You are wrong:
>open("tmp.txt" , "w").writelines ("%s\n" % (9*c) for c in "ABCDE")
instream = open("tmp.txt")
for line in instream:
... print instream.tell() , line.strip()
...
50 AAAAAAAAA
50 BBBBBBBBB
50 CCCCCCCCC
50 DDDDDDDDD
50 EEEEEEEEE
>>

Here's the workaround:
>instream = open("tmp.txt")
for line in iter(instream.r eadline, ""):
... print instream.tell() , line.strip()
...
10 AAAAAAAAA
20 BBBBBBBBB
30 CCCCCCCCC
40 DDDDDDDDD
50 EEEEEEEEE
>>

Peter
--
http://mail.python.org/mailman/listinfo/python-list
Sep 13 '07 #8

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

Similar topics

2
3415
by: Oxmard | last post by:
Armed with my new O'Reilly book Optimizing Oracle Performance I have been trying to get a better understanding of how Oracle works. The book makes the statement, " A database cal with dep=n + 1 is the recursive child of the first subsequent dep=n database call listed in the SQL data stream. The book gives a few examples, and in trying it out...
2
3930
by: Cigdem | last post by:
Hello, I am trying to parse the XML files that the user selects(XML files are on anoher OS400 system called "wkdis3"). But i am permenantly getting that error: Directory0: \\wkdis3\ROOT\home Canonicalpath-Directory4: \\wkdis3\ROOT\home\bwe\ You selected the file named AAA.XML getXmlAlgorithmDocument(): IOException Not logged in
8
1533
by: H | last post by:
Now, I'm here with another newbie question .... I want to read a text file, string by string (to do some things with some words etc etc), but I can't seem to find a way to do this String by String. Is there anyway, like String s = something.ReadString() ? Or what may be a fine way to do this ? Only thing I can some up with is to read 1...
13
1856
by: Ørjan Langbakk | last post by:
I wanna make a file that holds the complete pricelist for a small webshop (yes, I know that a database in the background would be a lot simpler, but that is not an option today, unfortunately). I'm thinking something like creating a file that holds the productname and the price, and then just get the price from the other pages that needs...
27
5103
by: comp.lang.tcl | last post by:
My TCL proc, XML_GET_ALL_ELEMENT_ATTRS, is supposed to convert an XML file into a TCL list as follows: attr1 {val1} attr2 {val2} ... attrN {valN} This is the TCL code that does this: set contents ]; close $fileID
5
3786
by: baskarpr | last post by:
Hi all, I my program after parsing in SAX parser, I want to write the parse result as an XML file. I want to ensure that there should be no difference between source XML file and parse result xml file. Because I set some properties in parser, which may cause to changes between actual and parsed. What I expect is the exact XML file structure...
5
5504
by: bmichel | last post by:
Hey, What I'm doing is the following: - Load XML data a file - Parsing the XML data - Printing some parsed content The problem is that the script execution is stopping before all the content is parsed and printed. Maybe the PHP is out of memory after a while. That would make sense
5
64603
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C++ programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
1
64039
AdrianH
by: AdrianH | last post by:
Assumptions I am assuming that you know or are capable of looking up the functions I am to describe here and have some remedial understanding of C programming. FYI Although I have called this article “How to Parse a File in C++”, we are actually mostly lexing a file which is the breaking down of a stream in to its component parts,...
7
2841
by: souravmallik | last post by:
Hello, I'm facing a big logical problem while writing a parser in VC++ using C. I have to parse a file in a chunk of bytes in a round robin fashion. Means, when I select a file, the parser will read first 512kb(IBUFFSIZE) of data, then move to next file and parse the same way. This way I can parse a number of file spreaded over different...
0
7568
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7858
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7614
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7928
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5469
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5185
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3615
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3598
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2066
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.