简体   繁体   中英

Creating dictionary from a csv file

I have a csv file as shown below:

EmpID ; Day ; Duty
521;0;1
521;1;1
521;2;0
521;3;0
521;4;1
521;5;1
521;6;0
521;7;0
521;8;1
521;9;1
521;10;0
521;11;0
521;12;1
521;13;1
879;0;1
879;1;0
879;2;1
879;3;0
879;4;1
879;5;0
879;6;1
879;7;0
879;8;1
879;9;0
879;10;1
879;11;0
879;12;1
879;13;1
978;0;1
978;1;1
978;2;0
978;3;1
978;4;1
978;5;1
978;6;0
978;7;1
978;8;1
978;9;1
978;10;1
978;11;1
978;12;0
978;13;1
979;0;1
979;1;1
979;2;1
979;3;0
979;4;1
979;5;1
979;6;1
979;7;0
979;8;1
979;9;1
979;10;1
979;11;1
979;12;0
979;13;1\

The first number indicates the ID of an employee, then the number of the day of the work schedule and the last number indicates whether the employee is working that day.

0: the employee is NOT working that day
1: the employee IS working that day

I want to write a dictionary that has the EmpID as key and another dictionary as the value. The inner dictionary should contain the Day as key and the Duty as value.

So this is what I have:

def schedule_dict(file):

    the_dict = csv.DictReader(file, delimiter = ';')

    schedule={}

    for item in the_dict:

        schedule[item['EmpID']] = {}

        for thing in the_dict:
     
            schedule[item['EmpID']].update({thing['Day'] : thing['Duty']})

            if int(thing['Day']) == 13:
                break
    
    return schedule

I have read the csv file with the DictReader command and I get this dictionary:

{
    '521': {'1': '1', '2': '0', '3': '0', '4': '1', '5': '1', '6': '0', '7': '0', '8': '1', '9': '1', '10': '0', '11': '0', '12': '1', '13': '1'},
    '879': {'1': '0', '2': '1', '3': '0', '4': '1', '5': '0', '6': '1', '7': '0', '8': '1', '9': '0', '10': '1', '11': '0', '12': '1', '13': '1'},
    '978': {'1': '1', '2': '0', '3': '1', '4': '1', '5': '1', '6': '0', '7': '1', '8': '1', '9': '1', '10': '1', '11': '1', '12': '0', '13': '1'},
    '979': {'1': '1', '2': '1', '3': '0', '4': '1', '5': '1', '6': '1', '7': '0', '8': '1', '9': '1', '10': '1', '11': '1', '12': '0', '13': '1'}
}

But I still need the first day that is number as zero ('0').

with open("your_file.csv", "r") as f:
    the_dict = list(map(lambda y: y.split(";"), [line.strip() for line in f]))
schedule = {}
for item in the_dict:
    if item[0] not in schedule.keys():
        schedule.setdefault(item[0], {item[1]: item[2]})
    else:
        schedule[item[0]].update({item[1]: item[2]})
print(schedule)
{
'521': {'0': '1', '1': '1', '2': '0', '3': '0', '4': '1', '5': '1', '6': '0', '7': '0', '8': '1', '9': '1', '10': '0', '11': '0', '12': '1', '13': '1'}
'879': {'0': '1', '1': '0', '2': '1', '3': '0', '4': '1', '5': '0', '6': '1', '7': '0', '8': '1', '9': '0', '10': '1', '11': '0', '12': '1', '13': '1'}
'978': {'0': '1', '1': '1', '2': '0', '3': '1', '4': '1', '5': '1', '6': '0', '7': '1', '8': '1', '9': '1', '10': '1', '11': '1', '12': '0', '13': '1'}
'979': {'0': '1', '1': '1', '2': '1', '3': '0', '4': '1', '5': '1', '6': '1', '7': '0', '8': '1', '9': '1', '10': '1', '11': '1', '12': '0', '13': '1'}
}

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