forked from shibing624/addressparser
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_demo.py
46 lines (37 loc) · 1.21 KB
/
cmd_demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# -*- coding: utf-8 -*-
"""
@author:XuMing(xuming624@qq.com)
@description:
"""
import os
import sys
sys.path.append('..')
import addressparser
def parse(addresses):
"""
Turns address list into province, city, country and street.
:param addresses: list of address
:return: list of province, city, country and street
"""
result = []
df = addressparser.transform(addresses, open_warning=False, cut=False)
for map_key in zip(df["省"], df["市"], df["区"], df["地名"]):
place = map_key[3]
if not isinstance(place, str):
place = ''
result.append('\t'.join([map_key[0], map_key[1], map_key[2], place]))
return result
if __name__ == '__main__':
origin_path = os.path.join(os.path.dirname(__file__), '../tests/addr.csv')
lines = []
with open(origin_path, 'r', encoding='utf-8') as f:
for line in f:
lines.append(line.strip())
print('{} lines in input'.format(len(lines)))
parsed = parse(lines)
count = 0
with open('addr_processed.txt', 'w', encoding='utf-8') as f:
for i, o in zip(lines, parsed):
count += 1
f.write(i + '\t' + o + '\n')
print('{} lines in output'.format(count))