Member Avatar for pizte

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)

Recommended Answers

All 2 Replies

I think you could check for a number in the second position, if I understand correctly that overlays use numbers and updates/adds use a letter.

if len(line.strip()) and \
   not line.startswith("Found") and \
   not line[1].isdigit():
commented: Clear and concise. Self explainable. +0
Member Avatar for pizte

I think you could check for a number in the second position, if I understand correctly that overlays use numbers and updates/adds use a letter.

if len(line.strip()) and \
   not line.startswith("Found") and \
   not line[1].isdigit():

Just the thing I was looking for, thank you. I didn't know about the startswith() and isdigit() functions.

Thanks :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.