Reputation: 347
From the following file, I'd like to extract only the names belonging to the 'Family2'.
For example,
key1 = 'Family'
key2 = '2'
so that I could get James, Emily, and Judy only.
Family1:
name: John
name: Rachel
name: Cindy
name: William
Family2:
name: James
name: Emily
name: Judy
Family3:
name: Steve
name: Olivia
name: Noah
name: William
Family4:
name: Rachel
Family5:
name: Mason
I could get up to the following.
key1 = 'Family'
key2 = '2'
found = False
matchedlines = []
with open('families.txt', 'r') as f:
for line in f:
if found == False:
if key1 in line and key2 in line:
found = True
else:
if key1 not in line:
matchedlines.append(line)
else:
found = False
print(matchedlines)
Output:
[' name: James\n', ' name: Emily\n', ' name: Judy\n']
I think my logic is ok but any advice on making this neater or more Pythonic would be appreciated.
Thanks in advance.
Upvotes: 0
Views: 46
Reputation: 13079
Some rather minor comments.
No need for if found == False
- it only ever contains a boolean so you can just use if not found
When checking for the start line, you probably want to test in such a way as to exclude e.g. Family12:
You can make use of elif
instead of having an else
block that just contains an if
block -- an if
statement can have any number of elif
blocks and then an optional else
block.
You are probably going to want to extract the names from the matched lines, after checking that they actually contain names (your code includes the blank line in the matched lines). The code below contains a suggestion for how to do that simply.
at the end of the Family2
section, instead of setting found = False
, you could just break from the loop entirely rather than bothering to process the rest of the file
four spaces per indentation level is probably more readable than 2 (used by PEP8 style guide)
key1 = 'Family'
key2 = '2'
key3 = 'name:'
found = False
names = []
with open('families.txt', 'r') as f:
for line in f:
if not found:
if key1 + key2 + ":" in line:
found = True
elif key1 in line:
break
elif key3 in line:
name = line.replace(key3, "").strip()
names.append(name)
print(names)
Upvotes: 1