[Tutor] help on dic creation
Brayden Zhao
braybebrave at gmail.com
Wed Oct 31 02:01:34 CET 2012
hello!
I am doing my homework now and I am kinda stuck. Could any of you help me out?
Here is the homework problem:
fieldict(filename) reads a file in DOT format and
returns a dictionary with the DOT CMPLID, converted to an
integer, as the key, and a tuple as the corresponding value
for that key. The format of the tuple is:
(manufacturer, date, crash, city, state)
where these tuple items have the following types:
manufacturer -- this comes from the MFR_NAME field in the DOT format
date -- this comes from the FAILDATE field in the DOT format,
but converted to a Python datetime.date object
crash -- this comes from the CRASH field in the DOT format,
but converted to a Python bool type (True for a crash)
city -- comes from the CITY field in the DOT format
state -- comes from the STATE field in the DOT format
should return: fieldict("DOT500.txt")[82]
('FORD MOTOR COMPANY', datetime.date(1995, 1, 1), False, 'MARBLE HEAD', 'MA')
and here are parts of the data:
1 958164 TOYOTA MOTOR CORPORATION TOYOTA LAND CRUISER 1994 19941223 N 0 0 SERVICE BRAKES, HYDRAULIC:ANTILOCK ARNOLD CA JT3DJ81W8R0 19950103 19950103 ABS SYSTEM FAILURE, AT 20MPH. TT EVOQ V
2 958156 TOYOTA MOTOR CORPORATION TOYOTA PASEO 1994 Y 19941226 N 0 0 PARKING BRAKE:CONVENTIONAL SAN JOSE CA JT2EL45U5R0 19950103 19950103 1 PARKED ON FLAT SURFACE EMERGENCY BRAKING ENGAGED VEHICLE ROLLED REARWARD. TT EVOQ V
3 958124 TOYOTA MOTOR CORPORATION TOYOTA COROLLA 1994 Y 19941128 N 0 0 AIR BAGS:FRONTAL PHOENIX AZ 19950103 19950103 UPON FRONTAL COLLISION, AIR BAG FAILED TO DEPLOY. VEHICLE CLASSIFIED AS TOTALED. PLEASE DESCRIBE DETAILS. TT EVOQ V
4 958122 NISSAN NORTH AMERICA, INC. NISSAN MAXIMA 1994 19950103 N 0 0 SUSPENSION TUCSON AZ JN1HJ01F4RT 19950103 19950103 THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION. TT EVOQ V
5 958122 NISSAN NORTH AMERICA, INC. NISSAN MAXIMA 1994 19950103 N 0 0 ENGINE AND ENGINE COOLING:ENGINE TUCSON AZ JN1HJ01F4RT 19950103 19950103 THE STRUT WAS BAD THERE IS A NOISE ON THE PASSENGER SIDE DOOR AND THE ENGINE LIGHT MALFUNCTION. TT EVOQ V
Here is my code and I dont know why my code is only reading the 500th line of the file. Thanks for your help!
import datetime
def boolean(S):
if S=="Y":
return True
return False
def fieldict(filename):
D={}
with open(filename) as FileObject:
for lines in FileObject:
linelist=lines.split('\t')
Key=linelist[0]
ValCity=(linelist[12]).strip()
ValState=linelist[13]
ValOne=linelist[2]
ValTwo=linelist[6]
ValThree=boolean(linelist[7])
D={Key:(ValOne, ValTwo, ValThree, ValCity,ValState)}
return D
print fieldict("DOT500.txt")
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20121030/6037ffbb/attachment.html>
More information about the Tutor
mailing list