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)
Attachments
[U] app-admin/eselect-wxwidgets (1.1@08/02/09 -> (~)1.2): Eselect module and wrappers for wxWidgets
[U] app-arch/rpm2targz (9.0.0.3g@08/01/09 -> (~)9.0.0.4g): Convert a .rpm file to a .tar.gz archive
[U] app-portage/portage-utils (0.2@08/01/09 -> (~)0.2.1): small and fast portage helper tools written in C
[U] dev-java/antlr (2.7.7@04/23/09 3.1.3-r1(3)@04/23/09 -> 2.7.7 3.1.3-r2(3)): A parser generator for C++, C#, Java, and Python
[U] dev-java/icedtea6-bin (1.4.1@08/01/09 -> (~)1.6.2!s): A Gentoo-made binary build of the icedtea6 JDK
[U] dev-java/java-config (2.1.7(2)@04/23/09 -> 1.3.7-r1 2.1.9-r1(2)): Java environment configuration tool
[U] dev-java/junit (3.8.2-r1@04/23/09 -> 3.8.2-r1 4.6(4)): Simple framework to write repeatable tests
[U] dev-lang/ocaml (3.10.2@09/02/09 -> (~)3.11.1): Fast modern type-inferring functional programming language descended from the ML family
[U] dev-libs/beecrypt (4.1.2-r2@09/06/09 -> (~)4.2.1): general-purpose cryptography library
[U] dev-libs/gdl (2.26.2@08/01/09 -> (~)2.28.1): The Gnome Devtool Libraries
[U] dev-perl/Locale-gettext (1.05@08/01/09 -> 1.05-r1): A Perl module for accessing the GNU locale utilities
[U] dev-python/evolution-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for Evolution and Evolution Data Server
[U] dev-python/gdl-python (2.19.1-r1@08/01/09 -> (~)2.19.1-r2!t): Python bindings for GDL
[U] dev-python/gnome-media-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for GNOME media profiles
[U] dev-python/gnome-python-desktop (2.26.0@05/27/09 -> (~)2.28.0): Meta build which provides python interfacing modules for some GNOME desktop libraries
[U] dev-python/gtksourceview-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for the gtksourceview (version 1.8) library
[U] dev-python/libgnomeprint-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for GNOME printing support
[U] dev-python/libgtop-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for the libgtop library
[U] dev-python/librsvg-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for the librsvg library
[U] dev-python/metacity-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for the Metacity window manager
[U] dev-python/nautilus-cd-burner-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for Nautilus CD/DVD burning
[D] dev-python/pymsn (0.3.3@07/14/09 -> ??): The library behind the msn connection manager: telepathy-butterfly
[U] dev-python/totem-python (2.26.0@08/01/09 -> (~)2.28.0!t): Python bindings for the Totem Playlist Parser
[U] dev-ruby/ruby-atk (0.19.0@08/01/09 -> (~)0.19.1): Ruby Atk bindings
[U] dev-ruby/ruby-gconf2 (0.19.0@08/01/09 -> (~)0.19.1): Ruby GConf2 bindings
[U] dev-ruby/ruby-gdkpixbuf2 (0.19.0@08/01/09 -> (~)0.19.1): Ruby GdkPixbuf2 bindings
[U] dev-ruby/ruby-gnome2 (0.19.0@08/01/09 -> (~)0.19.1): Ruby Gnome2 bindings
[U] dev-ruby/ruby-gnomecanvas2 (0.19.0@08/01/09 -> (~)0.19.1): Ruby GnomeCanvas2 bindings
[U] dev-ruby/ruby-gtk2 (0.19.0@08/01/09 -> (~)0.19.1): Ruby Gtk2 bindings
[U] dev-ruby/ruby-libart2 (0.19.0@09/02/09 -> (~)0.19.1): Ruby Libart2 bindings
[U] dev-ruby/ruby-libglade2 (0.19.0@08/01/09 -> (~)0.19.1): Ruby Libglade2 bindings
[U] dev-util/boost-build (1.37.0(1.37)@08/01/09 1.39.0(1.39)@08/01/09 -> 1.35.0-r2 (~)1.36.0(1.36) (~)1.37.0(1.37) (~)1.39.0(1.39) (~)1.40.0(1.40)): A system for large project software construction, which is simple to use and powerful.
[U] media-gfx/graphviz (2.24.0@09/02/09 -> 2.24.0-r2!t): Open Source Graph Visualization Software
[U] media-libs/libmtp (1.0.0@08/22/09 -> (~)1.0.1): An implementation of Microsoft's Media Transfer Protocol (MTP).
[U] media-libs/vamp-plugin-sdk (2.0@08/02/09 -> (~)2.1): Audio processing plugin system for plugins that extract descriptive information from audio data
[U] media-sound/jack-audio-connection-kit (0.116.2@08/02/09 -> (~)0.117.0): A low-latency audio server
[U] net-im/telepathy-mission-control (4.67@08/01/09 -> (~)5.2.5): Telepathy Mission Control
[U] sys-apps/devicekit-power (010[1]@08/01/09 -> (~)012-r1): D-Bus abstraction for enumerating power devices and querying history and statistics
[D] sys-kernel/gentoo-sources (2.6.28(2.6.28)@12/30/08 2.6.30-r4(2.6.30-r4)@07/26/09 2.6.30-r5(2.6.30-r5)@08/24/09 2.6.30-r6(2.6.30-r6)@09/03/09 2.6.31-r1(2.6.31-r1)@09/30/09 2.6.31-r3(2.6.31-r3)@10/23/09 2.6.31-r4(2.6.31-r4)@11/08/09 2.6.31-r6(2.6.31-r6)@11/15/09 -> 2.6.16-r13(2.6.16-r13)!b!s 2.6.25-r9(2.6.25-r9)!b!s 2.6.26-r4(2.6.26-r4)!b!s 2.6.27-r8(2.6.27-r8)!b!s 2.6.27-r10(2.6.27-r10)!b!s 2.6.28-r5(2.6.28-r5)!b!s 2.6.28-r6(2.6.28-r6)!b!s 2.6.29-r5(2.6.29-r5)!b!s 2.6.29-r6(2.6.29-r6)!b!s 2.6.30-r4(2.6.30-r4)!b!s 2.6.30-r5(2.6.30-r5)!b!s 2.6.30-r6(2.6.30-r6)!b!s 2.6.30-r7(2.6.30-r7)!b!s 2.6.30-r8(2.6.30-r8)!b!s (~)2.6.30-r9(2.6.30-r9)!b!s (~)2.6.31(2.6.31)!b!s (~)2.6.31-r1(2.6.31-r1)!b!s (~)2.6.31-r2(2.6.31-r2)!b!s (~)2.6.31-r3(2.6.31-r3)!b!s (~)2.6.31-r4(2.6.31-r4)!b!s (~)2.6.31-r5(2.6.31-r5)!b!s (~)2.6.31-r6(2.6.31-r6)!b!s): Full sources including the Gentoo patchset for the 2.6 kernel tree
[U] virtual/perl-Text-Balanced (2.0.0@12/08/08 -> (~)2.02): Virtual for Text-Balanced
[U] virtual/perl-version (0.76@12/08/08 -> (~)0.78): Perl extension for Version Objects
[U] x11-apps/bdftopcf (1.0.0@08/01/09 -> (~)1.0.2): X.Org bdftopcf application
[U] x11-apps/ccsm (0.8.2@04/17/09 -> (~)0.8.4): Compizconfig Settings Manager
[U] x11-apps/mesa-progs (7.4.1@08/01/09 -> 7.5.1): Mesa's OpenGL utility and demo programs (glxgears and glxinfo)
[U] x11-libs/libXvMC (1.0.4@09/29/09 -> (~)1.0.5): X.Org XvMC library
[U] x11-misc/xorg-cf-files (1.0.2@12/05/08 -> (~)1.0.3): Old Imake-related build files
[1] "gnome" /usr/local/portage/layman/gnome

Found 46 matches.

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():

Edited 7 Years Ago by woooee: n/a

Comments
Clear and concise. Self explainable.

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 :)

This question has already been answered. Start a new discussion instead.