-2

I have a text file that looks like this:

subjects ENGLISH, MATHS, SCIENCE
Joe, A, A, B
Dave, A, B, C
Will, D, D, E

And I want to put it into a dictionary using Python

{’Joe’:{’ENGLISH’:A,’MATHS’:A,’SCIENCE’:B},
’Dave’:{’ENGLISH’:A,’MATHS’:B,’SCIENCE’:C},
’Will’:{’ENGLISH’:D,’MATHS’:D,’SCIENCE’:E}}

How would I go about doing this in one dictionary?

asked Nov 28, 2022 at 13:44
1
  • 1
    That looks like standard CSV format, for which Python even ships with builtin parser. What trouble do you have using such tools for the task? Commented Nov 28, 2022 at 13:56

3 Answers 3

1

Assuming you have a file called file.txt with the following contents:

subjects ENGLISH, MATHS, SCIENCE
Joe, A, A, B
Dave, A, B, C
Will, D, D, E

Try using * unpacking:

results = {}
with open('file.txt') as file:
 _, *subjects = next(file).split(' ') # Read header row
 subjects = [s[:-1] for s in subjects] # Remove trailing comma/newline from subjects
 for line in file:
 if line != '\n': # Skip empty lines
 name, *grades = line.strip().split(', ')
 results[name] = dict(zip(subjects, grades))
print(results)

You can also define the subjects in code and skip the header row completely:

subjects = ['ENGLISH', 'MATHS', 'SCIENCE']
results = {}
with open('file.txt') as file:
 next(file) # Skip header row since we have defined subjects in code...
 for line in file:
 if line != '\n': # Skip empty lines
 name, *grades = line.strip().split(', ')
 results[name] = dict(zip(subjects, grades))
print(results)

Output:

{'Joe': {'ENGLISH': 'A', 'MATHS': 'A', 'SCIENCE': 'B'}, 'Dave': {'ENGLISH': 'A', 'MATHS': 'B', 'SCIENCE': 'C'}, 'Will': {'ENGLISH': 'D', 'MATHS': 'D', 'SCIENCE': 'E'}}
answered Nov 28, 2022 at 13:59
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you, but what if the subjects were standard so we could in theory remove that line and have the same output how would that work?
What do you mean by "standard"?
So instead of getting the subjects from the text file. We can assume the subjects in the code if that makes sense
@Anthillion Is my update to the answer what you mean?
Yes it is thank you so much. Also is there anyway to get rid of the apostrophes on the letters so instead of 'A' its just A?
0

We can read the file by using pd.read_csv() and convert the pd to a dictionary by using: df.to_dict('index')

answered Nov 28, 2022 at 13:53

Comments

-1

You could convert your text file to CSV

Name, ENGLISH, MATHS, SCIENCE
Joe, A, A, B
Dave, A, B, C
Will, D, D, E

Then use the pandas' library to read the CSV file and convert it into the dictionary.

>>> import pandas as pd
>>> pd.read_csv('file_path.csv',index_col='Name').transpose().to_dict()

{'Joe': {'ENGLISH': ' A', 'MATHS': ' A', 'SCIENCE': ' B'}, 'Dave': {'ENGLISH': ' A', 'MATHS': ' B', 'SCIENCE': ' C'}, 'Will': {'ENGLISH': ' D', 'MATHS': ' D', 'SCIENCE': ' E'}}

answered Nov 28, 2022 at 14:00

1 Comment

I can't change it to a CSV but is there a way in panda to it via a text file

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.