By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,525 Members | 1,634 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,525 IT Pros & Developers. It's quick & easy.

Adding keys to a dictionary

P: 3
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>,<GPA>

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>
so three lines per student, with commas between each of the fields.

Currently, the code I have is:
Expand|Select|Wrap|Line Numbers
  1. fh=open('input.txt','r')
  2. line=fh.readline()
  3. studentList=[]
  4. while line!='':
  5.     line=line.rstrip()
  6.     fields=line.split(',')
  7.     print fields #Here as a check to see what data is being processed
  8.     studentList.append(fields)
  9.     line=fh.readline()
  10. print 'The studentList is,\n',studentList
  11. studentDict={}
  12. for n in range(0,11,3):
  13.     firstName=studentList[n][0]
  14.     studentDict=[firstName]
  15. print studentDict 
  16. fh.close()
But that just gives me
Expand|Select|Wrap|Line Numbers
  1. ['Samuel', 'C.', 'Young', '9614']
  2. ['24', 'Overlook Park', 'Bethel', 'CT', '06801']
  3. ['3.04']
  4. ['Jacob', 'R.', 'Hintz', '1452']
  5. ['14', 'Main Street', 'Easton', 'PA', '18042']
  6. ['2.01']
  7. ['Chris', 'M.', 'Shafer', '8692']
  8. ['3', 'Cross Hill Road', ' Bethel', 'CT', '06801']
  9. ['2.89']
  10. ['Gerald', 'G.', 'Kenning', '7245']
  11. ['1', 'Irish Way', 'Palmer', 'PA', '18045']
  12. ['1.25']
  13. The studentList is,
  14. [['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']]
  15. ['Gerald']
So how do I convert these lists into a dictionary? Formatting of the dictionary should be along the lines of,
Expand|Select|Wrap|Line Numbers
  1. {'name':{'firstName':<firstName>,'lastName':<lastName>,'middleName':<middleName>},'boxNum':<boxNum>,'address':{'houseNum':<houseNum>,'streetName':<streetName>,'City':<city>,'state':<state>,'zipCode':<zipCode>},'GPA':<GPA>}
Oct 29 '08 #1
Share this Question
Share on Google+
1 Reply

Expert 100+
P: 469
This line of code
seems wrong. You are assigning a list to studentDict, so studentDict is now a list. I didn't take a close enough look at your code to see what it's doing, but this is the syntax for adding an item to a dictionary:
Expand|Select|Wrap|Line Numbers
  1. myDict = {}
  2. myDict["Key"] = "Item"
Now myDict is {"Key" : "Item"}.
I hope this is somewhat helpful.
Good luck.
Oct 29 '08 #2

Post your reply

Sign in to post your reply or Sign up for a free account.