fileinput.input, readlines and ...

P

Przemyslaw Bak

Hello,

I many files with log data. The structure of the file is quite
inconvenience and similar to the following example:
Data1
ValueA
Data2
ValueB
Data3
ValueC
....
To get the values I need to find Data* and then get to the next line.
I tried to use fileinput.input :
....
for line in fileinput.input(filename):
if line.find("Data2") >= 0:
(now what ?)

So I can find the requested line (Data2) but how to get value from the
next line ?
Is the fileinput.input the most convenient for this type of task ?
Maybe
readline or readlines would be better ?
What can you recommend ?

Regards
 
P

Peter Otten

Przemyslaw said:
Hello,

I many files with log data. The structure of the file is quite
inconvenience and similar to the following example:
Data1
ValueA
Data2
ValueB
Data3
ValueC
...
To get the values I need to find Data* and then get to the next line.
I tried to use fileinput.input :
...
for line in fileinput.input(filename):
if line.find("Data2") >= 0:
(now what ?)

So I can find the requested line (Data2) but how to get value from the
next line ?

lines = fileinput.input(filename)
for line in lines:
if "Data2" in line:
print line.strip(), "-->", next(lines).strip()
Is the fileinput.input the most convenient for this type of task ?
Maybe
readline or readlines would be better ?
What can you recommend ?

If you need more than a few name value pairs it pays to put the data in a
dictionary first:

# assuming that values always consist of a single line
with open(filename) as instream:
lines = (line.strip() for line in lines)
lookup = dict(zip(lines, lines))
print lookup["Data2"]
print lookup["Data3"]

Peter
 
P

Peter Otten

Scott said:
Peter Otten wrote:
Little bit of a fluff-up here.

Sorry, it should have been

with open(filename) as instream:
lines = (line.strip() for line in instream)
lookup = dict(zip(lines, lines))
Perhaps something like:

with open(filename) as instream:
lines = (line.strip() for line in instream)
lookup = dict(zip(lines[::2], lines[1::2]))

Tu quoque ;)

Peter
 
P

Private Private

lines = fileinput.input(filename)
for line in lines:
    if "Data2" in line:
        print line.strip(), "-->", next(lines).strip()

I get an error:

....
print line.strip(), "-->", next(lines).strip()
NameError: global name 'next' is not defined
 
P

Peter Otten

Private said:
I get an error:

...
print line.strip(), "-->", next(lines).strip()
NameError: global name 'next' is not defined


In Python versions prior to 2.6 instead of

next(lines)

you can use

lines.next()

Peter
 
P

Private Private

Hello,

I many files with log data. The structure of the file is quite

Each requested value is in separated file.
While traversing using os.path.walk I have noticed that I get files
unsorted.
Is it possible to get them sorted ?

przemol
 
K

Kushal Kumaran

Each requested value is in separated file.
While traversing using os.path.walk I have noticed that I get files
unsorted.
Is it possible to get them sorted ?

You can use the os.walk generator and sort the filenames in the returned tuple.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top