[Tutor] help on dic creation
Brett Ritter
swiftone at swiftone.org
Tue Nov 6 00:37:59 CET 2012
> Here is my code and I dont know why my code is only reading the 500th line of the file. Thanks for your help!
Let me offer you some hints:
This sounds like only the last line is getting saved into the dict.
Yet your loop is clearly going over each line. Ergo, the problem is
probably in the part where you add the line to the dict.
Focus on that part and see what is happening versus what you want to
have happening. Double check what command you are actually giving.
On Tue, Oct 30, 2012 at 6:01 PM, Brayden Zhao <braybebrave at gmail.com> wrote:
> 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")
>
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
--
Brett Ritter / SwiftOne
swiftone at swiftone.org
More information about the Tutor
mailing list