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)
fieldict("DOT500.txt")[416]
('DAIMLERCHRYSLER CORPORATION', datetime.date(1995, 1, 9), False, 'ARCADIA',
so far, I have tried
from collections import defaultdict
import datetime
def fieldict(filename):
with open(filename) as f:
x=[line.split('\t')[0].strip() for line in f] #list of complaint numbers
y= line.split('\t') #list of full complaints
d={}
for j in x:
Y= True
N= False
d[j] = tuple(y[2],datetime.date(y[7]), y[6], y[12], y[13]) #dict with number of complaint as key and tuple with index as values
return d
No luck… I think I am close..any help is greatly appreciated
EDIT: each complaint is formatted like this
'11\t958128\tDAIMLERCHRYSLER CORPORATION\tDODGE\tSHADOW\t1990\tY\t19941117\tN\t0\t0\tENGINE AND ENGINE COOLING:ENGINE\tWILMINGTON \tDE\t1B3XT44KXLN\t19950103\t19950103\t\t1\tENGINE MOTOR MOUNTS FAILED, RESULTING IN ENGINE NOISE. *AK\tEVOQ\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\tV\t\r\n'
Entry without character marks showing :
11 958128 DAIMLERCHRYSLER CORPORATION DODGE SHADOW 1990 Y 19941117 N 0 0 ENGINE AND ENGINE COOLING:ENGINE WILMINGTON DE 1B3XT44KXLN 19950103 19950103 1 ENGINE MOTOR MOUNTS FAILED, RESULTING IN ENGINE NOISE. *AK EVOQ
Looks like you want to make friends with the
csvmodule, as this looks like tab formatted csv text. Thecsv.reader()has a.next()method which is called when you throw it in a for loop, so you can go line by line through the file.As a general tip, read PEP8, and use understandable variable names.
With python, if it starts to feel hard that’s a good sign that there usually is a better way.