Skip to content
Advertisement

Creating sets of specific extracted values from a .txt file (Python)

I have a .txt file that says “NAMES,” “POINTS” and “SUMMARY” in capital letters, each followed by lines containing data. Each of these three groups is separated by an empty line:

NAMES
John Cena
Sam Smith
Selena Gomez

POINTS
sixteen
forty
thirty

SUMMARY
eighth place
sixth place
first place

My goal is to create three separate sets of names, points and summary.

I already created a set of names using the following code (which outputs a set of all names as intended):

names = set()

for line in open('handout_example.txt'):
    line = line.strip()
    if not line:
        break
    names.add(line)

names.remove('NAMES')
print(names) #this outputs a set of all names

However, I am unsure on how to create a set of points and a set of summary given they’re after an empty line and not at the start of the code unlike names. Any help would be greatly appreciated!! Thank you in advance <3

Advertisement

Answer

here is my solution:

names = set()
points = set()
summary = set()

next = 0

for line in open('handout_example.txt'):
    line = line.strip()
    if not line:
        next += 1
        continue
    if next == 0:
       names.add(line)
    elif next == 1:
       points.add(line)
    elif next == 2:
       summary.add(line)

names.remove('NAMES')
points.remove('POINTS')
summary.remove('SUMMARY')

print(f'{names}t{points}t{summary}')

It simple and could be done better but this will work for you I guess.

EDIT: more “pretty” vesrion:

nps = dict({'names': set(), 'points': set(), 'summary': set()})
nps_n = ['names', 'points', 'summary']

next = 0

for line in open('handout_example.txt'):
   line = line.strip()

   if not line:
      next += 1
      continue
   
   nps[nps[next]].append(line)
7 People found this is helpful
Advertisement