I have a string stored in a text file. I need to read the string into python as a list of dictionaries. The format of data in the text file (called "input.txt" ) is as follows:
<first name>,<last name>,<box number>,<house number>,<street >,<city>,<state >,<zipCode>,<GP A>
Then there are multiple students, which are just added as one long line.
The format that the information needs to be in when imported into python is as follows:
<first name>,<last name>,<box number>
<house number>,<street >,<city>,<state >,<zipCode>
<GPA>
so three lines per student, with commas between each of the fields.
Currently, the code I have is:
But that just gives me
So how do I convert these lists into a dictionary? Formatting of the dictionary should be along the lines of,
Thanks!
<first name>,<last name>,<box number>,<house number>,<street >,<city>,<state >,<zipCode>,<GP A>
Then there are multiple students, which are just added as one long line.
The format that the information needs to be in when imported into python is as follows:
<first name>,<last name>,<box number>
<house number>,<street >,<city>,<state >,<zipCode>
<GPA>
so three lines per student, with commas between each of the fields.
Currently, the code I have is:
Code:
fh=open('input.txt','r')
line=fh.readline()
studentList=[]
while line!='':
line=line.rstrip()
fields=line.split(',')
print fields #Here as a check to see what data is being processed
studentList.append(fields)
line=fh.readline()
print 'The studentList is,\n',studentList
studentDict={}
for n in range(0,11,3):
firstName=studentList[n][0]
studentDict=[firstName]
print studentDict
fh.close()
Code:
['Samuel', 'C.', 'Young', '9614'] ['24', 'Overlook Park', 'Bethel', 'CT', '06801'] ['3.04'] ['Jacob', 'R.', 'Hintz', '1452'] ['14', 'Main Street', 'Easton', 'PA', '18042'] ['2.01'] ['Chris', 'M.', 'Shafer', '8692'] ['3', 'Cross Hill Road', ' Bethel', 'CT', '06801'] ['2.89'] ['Gerald', 'G.', 'Kenning', '7245'] ['1', 'Irish Way', 'Palmer', 'PA', '18045'] ['1.25'] The studentList is, [['Samuel', 'C.', 'Young', '9614'], ['24', 'Overlook Park', 'Bethel', 'CT', '06801'], ['3.04'], ['Jacob', 'R.', 'Hintz', '1452'], ['14', 'Main Street', 'Easton', 'PA', '18042'], ['2.01'], ['Chris', 'M.', 'Shafer', '8692'], ['3', 'Cross Hill Road', ' Bethel', 'CT', '06801'], ['2.89'], ['Gerald', 'G.', 'Kenning', '7245'], ['1', 'Irish Way', 'Palmer', 'PA', '18045'], ['1.25']] ['Gerald']
Code:
{'name':{'firstName':<firstName>,'lastName':<lastName>,'middleName':<middleName>},'boxNum':<boxNum>,'address':{'houseNum':<houseNum>,'streetName':<streetName>,'City':<city>,'state':<state>,'zipCode':<zipCode>},'GPA':<GPA>}
Comment