Sunday, March 13, 2022

[SOLVED] How to parse a zcat output

Issue

What is the easiest way to split a string by \n\n in python?

This is my raw string which comes from zcat output of href="http://ftp2.de.debian.org/debian/dists/stable/main/binary-i386/Packages.gz" rel="nofollow">http://ftp2.de.debian.org/debian/dists/stable/main/binary-i386/Packages.gz

Package: zziplib-bin\n
Priority: optional\n
Section: utils\n
Installed-Size: 116\n
Maintainer: LIU Qi <[email protected]>\n
Architecture: i386\n
Source: zziplib (0.13.56-1)\n
Version: 0.13.56-1+b1\n
...\n
\n
Package: zzuf\n
Priority: optional\n
Section: devel\n
Installed-Size: 228\n
Maintainer: Sam Hocevar <[email protected]>\n
Architecture: i386\n
Version: 0.13.svn20100215-2\n
...\n
\n

Perhaps any of you have ever done this

This should ideally look like this:

{
"zziplib-bin": {"Version": "0.13.56-1+b1"},
"zzuf": {"Version": "0.13.svn20100215-2"},
...
}

- Timo


Solution

I figured it out. Maybe someone can make better suggestions on that.

a = zcat_output.split('\n\n')
a.pop(len(a)-1)
c = dict()
for x in [x.splitlines() for x in a]:
    pkg = ""
    for y in [y for y in x if y.startswith('Package:') or y.startswith('Version:')]:
        z = y.split(':')
        if z[0] == 'Package':
            pkg = z[1].strip()
        else:
            c[pkg] = {z[0]: z[1].strip()}

print c.get('whois')

Output:

{'Version': '5.0.10'}


Answered By - tuna
Answer Checked By - Katrina (WPSolving Volunteer)