Przemyslaw Bak wrote: > 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 -- http://mail.python.org/mailman/listinfo/python-list