Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from datetime import datetime
- import xlrd
- import re;
- from itertools import filterfalse
- def formater_phone(string):
- if string == None:
- return None
- phone = ''.join(x for x in string if x.isdigit())
- logging.warning(phone)
- return phone
- def purge_dublicates(X):
- unique_X = []
- for i, row in enumerate(X):
- if row not in X[i + 1:]:
- unique_X.append(row)
- return unique_X
- def drop_string(string):
- """Картина под названием 'Говнокод в серых тонах'"""
- if len(string) == 4:
- _s = str(string[0]) + ' ' + str(string[1])
- _q = str(string[2]) + ' ' + str(string[3])
- string = _s + ', ' + _q
- if len(string) == 3:
- _s = str(string[1]) + str(string[2])
- string = 'д ' + string[0] + ', '+ _s
- if len(string) == 5:
- if not string[1].isdigit():
- string = 'д ' + str(string[0]) + ' , кв' + str(string[2])
- else:
- _s = str(string[0]) + ' ' + str(string[1])
- _q = str(string[2]) + ' ' + str(string[3])
- string = _s + ', ' + _q
- if len(string) == 1:
- try:
- res = string[0].split('-')
- string = str(res[0]) + ', кв ' + str(res[1])
- except:
- string = string[0]
- if len(string) == 0:
- string = None
- if len(string) == 2:
- string = ','.join(string)
- return string
- # res = string.split('-')
- # return str(res[0]) + ', кв ' + str(res[1])
- # except Exception as e:
- # print(string)
- # return string.split('-')[0]
- def convert_to_normal(string):
- # try:
- address = str(string[0]) + ', '+ str(string[1]).capitalize() + ', ' + str(drop_string(string[2:]))
- # address = ', '.join([string[0], string[1], drop_string(string[2:])])
- # except Exception as e:
- # print(e)
- # address = ''
- return address
- def formater_address(string):
- string = re.sub(r'\d{6}', '', string)
- if not re.search('Самара', string):
- return None
- string = string.replace(' ', ' ')
- # string = string.replace('д.', ',')
- list_exclude = ['cамарская','г.', ' ',' ','пр-кт',',','область','ул','ш.','р-н', 'р-н.','.','Россия','обл.', 'Самарская', 'УЛ.', 'УЛ', 'домофон','обл', ' ',' ','этаж','поъез','поъезд','подъезд','ПР-КТ','не работает','Страна','Промыленный','Промышленный','ПР','Красноглинский','промыленный']
- for i in list_exclude:
- string = string.replace(i, '')
- string = string.strip()
- string = string.split(' ')
- while '' in string:
- string.remove('')
- string = purge_dublicates(string)
- list_exclude = ['Карла','КАРЛА','промышленный','п','ос', 'г','км','Стара','вор','СЗАГОРА','ШОС','эт']
- for i,val in enumerate(list_exclude, start=0):
- for j, vals in enumerate(string):
- if val == vals:
- del string[j]
- if string[1].isdigit():
- new_element = str(string[1]) + ' ' + str(string[1+1])
- num = 1
- del string[num]
- del string[num]
- string.insert(1, new_element)
- comlite_address = convert_to_normal(string)
- return comlite_address
- def parse_data_exel(FILE):
- """Метод парсит документ и возвращает список"""
- # try:
- # Экземпляр документа
- lists = []
- rb = xlrd.open_workbook(FILE,on_demand = True)
- sheet = rb.sheet_by_index(0)
- for rownum in range(sheet.nrows):
- row = sheet.row_values(rownum)
- lists.append(row)
- pacient_list = []
- for i, val in enumerate(lists, start=1):
- if i > 1:
- address = formater_address(val[5])
- try:
- phone = formater_phone(val[7])
- except Exception as e:
- phone = 80000000000
- y, m, d, h, i, s = xlrd.xldate_as_tuple(val[2], rb.datemode)
- date_of_birth = '{0}-{1}-{2}'.format(y, m, d)
- if address != None:
- pacient_list.append({
- 'FIO' : val[1],
- 'date_birth' : date_of_birth,
- 'address' : address,
- 'addition' : val[6],
- 'phone' : int(phone),
- 'reason' : val[12],
- })
- return pacient_list
- # except Exception as e:
- # logging.info('Error parse document - %s' % (e))
- # print('Error parse document - %s' % (e))
- pacient = parse_data_exel('call.xls')
- for i, val in enumerate(pacient):
- print("%s" % (pacient[i]['address']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement