Created
August 7, 2018 10:44
-
-
Save ap-Codkelden/c408fb47046602b0a37c461bc6d00400 to your computer and use it in GitHub Desktop.
forms2csv.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
# -*- coding: utf-8 -*- | |
import csv | |
CSV_FILE = 'F123001' | |
REGION = {'Україна': '99','АР Крим': '01','Вінницька': '02', | |
'Волинська': '03','Дніпропетровська': '04','Донецька': '05','Житомирська': '06','Закарпатська': '07','Запорізька': '08','Івано-Франківська': '09','Київська': '10','Кіровоградська': '11','Луганська': '12','Львівська': '13','Миколаївська': '14','Одеська': '15','Полтавська': '16','Рівненська': '17','Сумська': '18','Тернопільська': '19','Харківська': '20','Херсонська': '21','Хмельницька': '22','Черкаська': '23','Чернівецька': '24','Чернігівська': '25','м.Київ': '26','м.Севастополь': '27','м. Київ': '26','м. Севастополь': '27',} | |
with open(CSV_FILE + '.csv') as csvfile: | |
reader = csv.reader(csvfile, delimiter=';', quotechar='"') | |
data = [row for row in reader] | |
container = [] | |
for r in data: | |
if r[0] and '.' in r[0]: | |
_ = r[0].split('.') | |
if len(_[1]) != 2: | |
_[1] = '0' + _[1] | |
code = _[0] + _[1] | |
elif r[0]: | |
code = r[0] | |
try: | |
state = REGION[r[1]] | |
except KeyError: | |
pass | |
if not r[2]: | |
continue | |
nrow = ['2017','0','00','0',state,'0','0'] | |
nrow.append(code) | |
nrow.extend(r[2:]) | |
container.append(nrow) | |
with open(CSV_FILE + 'e17' + '.csv', 'w') as csvfile: | |
writer = csv.writer(csvfile, delimiter=';', quotechar='"', | |
quoting=csv.QUOTE_MINIMAL) | |
writer.writerows(container) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment