简体   繁体   中英

trying to create a dictionary from a text file

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    

Note: Trimming the newline is left up to the reader.

A clean way of accomplishing this is to use dict(zip(headers,data_list))

Presuming your sample data looks like

joe\tSan Francisco\tapple
frank\tNew York City\torange
tim\tHawaii\tpineapple

You could do something like:

results = []
headers = ['person','place','fruit']

for line in open('datafile.txt').readlines():
    record = line.split('\t')
    results.append(dict(zip(headers,record)))

Which will make a dict for each line and append it to the end of 'results'.

Looking like:

[{'fruit': 'apple\n', 'person': 'joe', 'place': 'San Francisco'},
 {'fruit': 'orange\n', 'person': 'frank', 'place': 'New York City'},
 {'fruit': 'pineapple\n', 'person': 'tim', 'place': 'Hawaii'}]

Looks like you want to make friends with the csv module, as this looks like tab formatted csv text. The csv.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.

import csv
import datetime

def _build_datetime(line)
    year_idx = x
    month_idx = y
    day_idx = z
    indexes = (year_idx, month_idx, day_idx)

    result_datetime = None
    if all(line[idx] for idx in indexes): # check that expected values are populated
        int_values = [int(line[idx]) for idx in indexes]
        result_datetime = datetime.date(*int_values)
    return result_datetime

def format2dict(filename):
    complaints = {}
    with open(filename, "rb") as in_f:
        reader = csv.reader(in_f, delimiter='\t')
        complaint_id_idx = 0
        manufacturer_idx = 2
        crash_idx = x
        city_idx = 12
        state_idx = 13

        for line in reader:
            complaint_id = int(line[complaint_id_idx])
            data= (
                         line[manufacturer_idx], 
                         _build_datetime(line),
                         line[crash_idx],
                         line[city_idx],
                         line[state_idx],
                        )

            complaints[complaint_id] = data
    return complaints


if __name__ == "__main__":
    formatted_data = format2dict("DOT500.txt")

You're on the right track with line.split('\\t') to break up text into pieces. Try something like this to build up the tuple from the split pieces.

import datetime

a = '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'

fields = a.split('\t')
recordNum = fields[0]
mfr = fields[2]
recDate = datetime.date(int(fields[5]),1,2)
make = fields[4]
DOTrecord = recordNum,mfr, recDate,make
print DOTrecord

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM