Hello, I'm having trouble making a little parser for the eix program (a gentoo portage search tool)
I need to delete (or not write) the last lines of the output (overlays and total packages), they will start always like '[n]' where n is the number of the overlay and the total packages starts by 'Found'. This is the current code I have, but as you can see, it only deletes if the overlay matched is the [1]:
As you will seee, this code is badly written, I must say I'm not even a programmer but a photographer :). I attached to this message an example output of the eix command.
from subprocess import Popen
import fileinput
# Execute eix command and send output to a file
a = Popen('eix -uc >> reportfile.txt', shell=True)
# Wait until command is completed
a.wait()
# Start line count
p = 0
# We open the file and edit itself with the fileinput
# module, this way we don't need two files
for line in fileinput.input('reportfile.txt', inplace=1):
p += 1
# Here we split the line content, so we can get
# the package name by package[1]
package = line.split(' ')
# If [1] not found in the line we continue. This
# method does not allow (or at least it didn't work
# for me) doing 'if X or X or X not in line'
if '[1]' not in line:
if 'Found' not in line:
try:
# The output is redirected to the file so
# it will print in the file
print package[1]
# Since the last line will give an error, we
# get it and write the last line
except:
# The counter does not count packages but lines
# so we substract the extra lines
print 'Packages that will be updated: ' + str(p - 2)