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

Question:

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

Asked By: kitty_cake

||

Answers:

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)
Answered By: JohnyCapo

I would try to detect if you see an empty line or the title line so you can then choose the appropriate type. For example:

data_types = {"NAMES":set(), "POINTS":set(), "SUMMARY":set()}

current_key = None

Then in your loop, check if the line matches one of the data types (and is not an empty line). If it does, set the current_key to that value and add to the set in the correct spot in the dictionary.

Answered By: StonedTensor
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.