I’m reading a file and each line has a tag, followed by a colon and then the information that I want. A sample file would look like
Package: com.something.something
Section: Utilities
Name: Something
etc, (It’s an apt packages index if you’re wondering)
so I want to loop through each line and see if that line starts with an element from a list. I was thinking something like
PkgInfo={}
Tags=['Package', 'Section', 'Name']
for line in reader.readlines()
if line.startswith(element in Tags):
PkgInfo[element]=line.split(': ')[1]
This code doesn’t work, but hopefully you understand what I am trying to do. How would I go about accomplishing this?
Working solution with slightly different logic:
And pay attention to the fact that iteration over elements was not only one problem. ‘Package’ in
Tagswas defined as ‘Package: ‘,Tagsin loop referenced astags,split.lineinsteadline.split(), value isn’t stripped.