473,387 Members | 3,684 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Adding keys to a dictionary

telgroc
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>
<GPA>
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()
  17.  
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']
  16.  
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>}
Thanks!
Oct 29 '08 #1
1 2088
boxfish
469 Expert 256MB
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:
Expand|Select|Wrap|Line Numbers
  1. myDict = {}
  2. myDict["Key"] = "Item"
  3.  
Now myDict is {"Key" : "Item"}.
I hope this is somewhat helpful.
Good luck.
Oct 29 '08 #2

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

Similar topics

57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
9
by: Tim N. van der Leeuw | last post by:
Hi, I'd like to remove keys from a dictionary, which are not found in a specific set. So it's kind of an intersection-operation. I can create a new dictionary, or a loop over all keys and test...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
7
by: ProvoWallis | last post by:
I'm still learning python so this might be a crazy question but I thought I would ask anyway. Can anyone tell me if it is possible to join two dictionaries together to create a new dictionary using...
14
by: vatamane | last post by:
This has been bothering me for a while. Just want to find out if it just me or perhaps others have thought of this too: Why shouldn't the keyset of a dictionary be represented as a set instead of a...
22
by: bearophileHUGS | last post by:
>From this interesting blog entry by Lawrence Oluyede: http://www.oluyede.org/blog/2006/07/05/europython-day-2/ and the Py3.0 PEPs, I think the people working on Py3.0 are doing a good job, I am...
24
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to...
11
by: John | last post by:
I am coding a radix sort in python and I think that Python's dictionary may be a choice for bucket. The only problem is that dictionary is a mapping without order. But I just found that if the...
4
by: =?utf-8?B?TWFjaWVqIEJsaXppxYRza2k=?= | last post by:
Hi Pythonistas! I've got a question about storing tuples in a dictionary. First, a small test case which creates a list of dictionaries: import time list_of_dicts = keys = prev_clk =...
13
by: Nader | last post by:
Hello, I have a dictionary and will get all keys which have the same values. d = {('a' : 1), ('b' : 3), ('c' : 2),('d' : 3),('e' : 1),('f' : 4)} I will something as : d.keys(where their...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.