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: -
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 -
['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, - {'name':{'firstName':<firstName>,'lastName':<lastName>,'middleName':<middleName>},'boxNum':<boxNum>,'address':{'houseNum':<houseNum>,'streetName':<streetName>,'City':<city>,'state':<state>,'zipCode':<zipCode>},'GPA':<GPA>}
Thanks!
1 2088
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: -
myDict = {}
-
myDict["Key"] = "Item"
-
Now myDict is {"Key" : "Item"}.
I hope this is somewhat helpful.
Good luck.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Egor Bolonev |
last post by:
why functions created with lambda forms cannot contain statements?
how to get unnamed function with statements?
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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 =...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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...
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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,...
|
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...
|
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...
| |