0
empList="""201911007,James,Butt,Accounting,365;
201203008,Josephine,Darakjy,Marketing,365;"""

def createFiles():
    f = open("empList.txt","w")
    f.write(empList)
    f.close
num=int(input('Enter Employee Number: '))
print('''Name:
Department:
Rate: 
''')

How do I turn empList, which is a textfile, into a dictionary?

output should be

Enter Employee Number: 201911007
Name: James Butt
Department: Accounting
Rate: 365
  • 1
    See [Creating a dictionary from a csv file?](https://stackoverflow.com/questions/6740918/creating-a-dictionary-from-a-csv-file) Also `empList` is a string, not a text file. – jarmod Dec 14 '22 at 00:08
  • To specify, you want to READ a text file and store the contents in a dictionary. OR do you want to input data in the console and to store it on a file? – Eduard Dec 14 '22 at 00:56

1 Answers1

0

You can use the inbuilt csv library in Python

import csv
with open('empList.txt', 'r', newline='\n') as csvfile:
    field_names = ['employee_number', 'first_name', 'last_name', 'department', 'rate']
    reader = csv.DictReader(csvfile, field_names)
    for row in reader:
        print(row)

Output:

{'employee_number': '201911007', 'first_name': ' James', 'last_name': ' Butt', 'department': ' Accounting', 'rate': ' 365'}
{'employee_number': '201203008', 'first_name': ' Josephine', 'last_name': ' Darakjy', 'department': ' Marketing', 'rate': ' 365'}