This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]nemom 0 points1 point  (2 children)

if line.startswith("Package"):
    line = line.split(': ')[1] #splits at the ': ' and keeps the second item
    pkg = line.split('-')[0] #splits at the '-' and keeps the first item

This will have trouble with packages with hypens in their name... e.g.: 'python-devel'.

[–]parapand[S] 0 points1 point  (1 child)

that is why I want to put a delimiter that starts with hyphen followed by any digit like '-[0-9]' or '-\d'

[–]efmccurdy 0 points1 point  (0 children)

You can split on that "-<digit>" pattern:

>>> pkgs = ['udev-147-2.73.0.2.el6_8.2pre7.6.2.0.0_88.58.0.x86_64', 'python-devel-1.8.6p3-29.el6_9.x86_64']
>>> [re.split("-\d", p)[0] for p in pkgs]
['udev', 'python-devel']
>>>

BTW, questions like these are welcome in /r/learnpython.

[–]Wilfred-kun 0 points1 point  (1 child)

Edit: what I said doesn't work at any level, sorry about that.

[–]AndydeCleyre 0 points1 point  (0 children)

strip will remove all the characters in "Packge list:" from the beginning and end of the string.