43FUN
@43FUN
python/django developer

Как заменить список на словарь?

import csv
import json

csvfile = open('code.csv', 'r')
jsonfile = open('code.json', 'w')

fieldnames = ('year','1','2','3','4','5','6','7','8','9','10','11','12')
reader = csv.DictReader(csvfile, fieldnames)
next(reader)
for row in reader:
....data = {}
....data[row['year']] = {'1':row['1'].split(','),
..................................'2':row['2'].split(','),
..................................'3':row['3'].split(','),
..................................'4':row['4'].split(','),
..................................'5':row['5'].split(','),
..................................'6':row['6'].split(','),
..................................'7':row['7'].split(','),
..................................'8':row['8'].split(','),
..................................'9':row['9'].split(','),
..................................'10':row['10'].split(','),
..................................'11':row['11'].split(','),
..................................'12':row['12'].split(',')}
....json.dump(data, jsonfile)
....jsonfile.write('\n')

код конвертирует csv в json, получается словарь такого вида

{'2018': {'11': ['3', '4', '5', '10', '11', '17', '18', '24', '25'], '10': ['6', '7', '13', '14', '20', '21', '27', '28'], '12': ['1', '2', '8', '9', '15', '16', '22', '23', '29', '30', '31*'], '1': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '13', '14', '20', '21', '27', '28'], '3': ['3', '4', '7*', '8', '10', '11', '17', '18', '24', '25', '31'], '2': ['3', '4', '10', '11', '17', '18', '22*', '23', '24', '25'], '5': ['1', '5', '6', '8*', '9', '12', '13', '19', '20', '26', '27'], '4': ['1', '7', '8', '14', '15', '21', '22', '28', '29', '30*'], '7': ['1', '7', '8', '14', '15', '21', '22', '28', '29'], '6': ['2', '3', '9', '10', '11*', '12', '16', '17', '23', '24', '30'], '9': ['1', '2', '8', '9', '15', '16', '22', '23', '29', '30'], '8': ['4', '5', '11', '12', '18', '19', '25', '26']}}

а нужно как то сделать такой

{'date': {'2018': {'11': {'3':{isWorking:2}, '4':{isWorking:2}, '5':{isWorking:2}, '10':{isWorking:2}....

csv здесь data.gov.ru/opendata/7708660670-proizvcalendar/dat...

Заранее спасибо.
  • Вопрос задан
  • 250 просмотров
Решения вопроса 1
@Dronablo
Oracle performance geek
import datetime
import json

in_data = {}
in_data['2018']={'11': ['3', '4', '5', '10', '11', '17', '18', '24', '25'],
               '10': ['6', '7', '13', '14', '20', '21', '27', '28'],
               '12': ['1', '2', '8', '9', '15', '16', '22', '23', '29', '30', '31*'],
               '1': ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '13', '14', '20', '21', '27', '28'],
               '3': ['3', '4', '7*', '8', '10', '11', '17', '18', '24', '25', '31'],
               '2': ['3', '4', '10', '11', '17', '18', '22*', '23', '24', '25'],
               '5': ['1', '5', '6', '8*', '9', '12', '13', '19', '20', '26', '27'],
               '4': ['1', '7', '8', '14', '15', '21', '22', '28', '29', '30*'],
               '7': ['1', '7', '8', '14', '15', '21', '22', '28', '29'],
               '6': ['2', '3', '9', '10', '11*', '12', '16', '17', '23', '24', '30'],
               '9': ['1', '2', '8', '9', '15', '16', '22', '23', '29', '30'],
               '8': ['4', '5', '11', '12', '18', '19', '25', '26']}

print(json.dumps(dict([[year, 
                  dict([[month, 
                         dict([[day, 
                                {'isWorking':2}] for day in in_data[year][month]])
                         ] for month in in_data[year]]
                      )] for x in in_data]
               ), indent=4, sort_keys=4
          ))


{
    "2018": {
        "1": {
            "1": {
                "isWorking": 2
            },
            "10": {
                "isWorking": 2
            },
            "13": {
                "isWorking": 2
            },
            "14": {
                "isWorking": 2
            },
            "2": {
                "isWorking": 2
            },
            "20": {
                "isWorking": 2
            },
            "21": {
                "isWorking": 2
            },
            "27": {
                "isWorking": 2
            },
            "28": {
                "isWorking": 2
            },
            "3": {
                "isWorking": 2
            },
            "4": {
                "isWorking": 2
            },
            "5": {
                "isWorking": 2
            },
            "6": {
                "isWorking": 2
            },
            "7": {
                "isWorking": 2
            },
            "8": {
                "isWorking": 2
            },
            "9": {
                "isWorking": 2
            }
        },
        "10": {
            "13": {
                "isWorking": 2
            },
            "14": {
                "isWorking": 2
            },
            "20": {
                "isWorking": 2
            },
            "21": {
                "isWorking": 2
            },
            "27": {
                "isWorking": 2
            },
            "28": {
                "isWorking": 2
            },
            "6": {
                "isWorking": 2
           },
            "7": {
                "isWorking": 2
            }
        },
        "11": {
            "10": {
                "isWorking": 2
            },
            "11": {
                "isWorking": 2
            },
            "17": {
                "isWorking": 2
            },
            "18": {
                "isWorking": 2
            },
            "24": {
                "isWorking": 2
            },
            "25": {
                "isWorking": 2
            },
            "3": {
                "isWorking": 2
            },
            "4": {
                "isWorking": 2
            },
            "5": {
                "isWorking": 2
            }
        },
        "12": {
            "1": {
                "isWorking": 2
            },
            "15": {
                "isWorking": 2
            },
            "16": {
                "isWorking": 2
            },
            "2": {
                "isWorking": 2
            },
            "22": {
                "isWorking": 2
            },
            "23": {
                "isWorking": 2
            },
            "29": {
                "isWorking": 2
            },
            "30": {
                "isWorking": 2
            },
            "31*": {
                "isWorking": 2
            },
            "8": {
                "isWorking": 2
            },
            "9": {
                "isWorking": 2
            }
        },
        "2": {
            "10": {
                "isWorking": 2
            },
            "11": {
                "isWorking": 2
            },
            "17": {
                "isWorking": 2
            },
            "18": {
                "isWorking": 2
            },
            "22*": {
                "isWorking": 2
            },
            "23": {
                "isWorking": 2
            },
            "24": {
                "isWorking": 2
            },
            "25": {
                "isWorking": 2
            },
            "3": {
                "isWorking": 2
            },
            "4": {
                "isWorking": 2
            }
        },
        "3": {
            "10": {
                "isWorking": 2
            },
            "11": {
                "isWorking": 2
            },
            "17": {
                "isWorking": 2
            },
            "18": {
                "isWorking": 2
            },
            "24": {
                "isWorking": 2
            },
            "25": {
                "isWorking": 2
            },
            "3": {
                "isWorking": 2
            },
            "31": {
                "isWorking": 2
            },
            "4": {
                "isWorking": 2
            },
            "7*": {
                "isWorking": 2
            },
            "8": {
                "isWorking": 2
            }
        },
        "4": {
            "1": {
                "isWorking": 2
            },
            "14": {
                "isWorking": 2
            },
            "15": {
                "isWorking": 2
            },
            "21": {
                "isWorking": 2
            },
            "22": {
                "isWorking": 2
            },
            "28": {
                "isWorking": 2
            },
            "29": {
                "isWorking": 2
            },
            "30*": {
                "isWorking": 2
            },
            "7": {
                "isWorking": 2
            },
            "8": {
                "isWorking": 2
            }
        },
        "5": {
            "1": {
                "isWorking": 2
            },
            "12": {
                "isWorking": 2
            },
            "13": {
                "isWorking": 2
            },
            "19": {
                "isWorking": 2
            },
            "20": {
                "isWorking": 2
            },
            "26": {
                "isWorking": 2
            },
            "27": {
                "isWorking": 2
            },
            "5": {
                "isWorking": 2
            },
            "6": {
                "isWorking": 2
            },
            "8*": {
                "isWorking": 2
            },
            "9": {
                "isWorking": 2
            }
        },
        "6": {
            "10": {
                "isWorking": 2
            },
            "11*": {
                "isWorking": 2
            },
            "12": {
                "isWorking": 2
            },
            "16": {
                "isWorking": 2
            },
            "17": {
                "isWorking": 2
            },
            "2": {
                "isWorking": 2
            },
            "23": {
                "isWorking": 2
            },
            "24": {
                "isWorking": 2
            },
            "3": {
                "isWorking": 2
            },
            "30": {
                "isWorking": 2
            },
            "9": {
                "isWorking": 2
            }
        },
        "7": {
            "1": {
                "isWorking": 2
            },
            "14": {
                "isWorking": 2
            },
            "15": {
                "isWorking": 2
            },
            "21": {
                "isWorking": 2
            },
            "22": {
                "isWorking": 2
            },
            "28": {
                "isWorking": 2
            },
            "29": {
                "isWorking": 2
            },
            "7": {
                "isWorking": 2
            },
            "8": {
                "isWorking": 2
            }
        },
        "8": {
            "11": {
                "isWorking": 2
            },
            "12": {
                "isWorking": 2
            },
            "18": {
                "isWorking": 2
            },
            "19": {
                "isWorking": 2
            },
            "25": {
                "isWorking": 2
            },
            "26": {
                "isWorking": 2
            },
            "4": {
                "isWorking": 2
            },
            "5": {
                "isWorking": 2
            }
        },
        "9": {
            "1": {
                "isWorking": 2
            },
            "15": {
                "isWorking": 2
            },
            "16": {
                "isWorking": 2
            },
            "2": {
                "isWorking": 2
            },
            "22": {
                "isWorking": 2
            },
            "23": {
                "isWorking": 2
            },
            "29": {
                "isWorking": 2
            },
            "30": {
                "isWorking": 2
            },
            "8": {
                "isWorking": 2
            },
            "9": {
                "isWorking": 2
            }
        }
    }
}
Ответ написан
Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы