Adding keys to a dictionary

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • telgroc
    New Member
    • Oct 2008
    • 3

    Adding keys to a dictionary

    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:
    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()
    But that just gives me
    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']
    So how do I convert these lists into a dictionary? Formatting of the dictionary should be along the lines of,
    Code:
    {'name':{'firstName':<firstName>,'lastName':<lastName>,'middleName':<middleName>},'boxNum':<boxNum>,'address':{'houseNum':<houseNum>,'streetName':<streetName>,'City':<city>,'state':<state>,'zipCode':<zipCode>},'GPA':<GPA>}
    Thanks!
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    This line of code
    studentDict=[firstName]
    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:
    Code:
    myDict = {}
    myDict["Key"] = "Item"
    Now myDict is {"Key" : "Item"}.
    I hope this is somewhat helpful.
    Good luck.

    Comment

    Working...