python行政区域代码转换

作用

中国行政区域代码和地址互相转换

代码

address.py

import jieba
import csv


class AddressCode(object):
    __address_dict = {}
    __code_dict = {}

    def __init__(self):
        with open('行政区域对照表.CSV') as csvfile:
            reader = csv.reader(csvfile)
            for item in reader:
                self.__address_dict[item[0]] = {
                    'city': item[1],
                    'family': item[2],
                    'level': item[3]
                }
                self.__code_dict[item[1]] = item[0]
        jieba.initialize()

    def __c2a(self, code, child=[]):
        try:
            item = self.__address_dict['%s' % code]
        except KeyError:
            return None
        child.insert(0, item['city'])
        if item['level'] == '1':
            return child
        return self.__c2a(item['family'], child)
    
    def address_to_list(self, address):
        words = list(jieba.cut(address))
        if len(words) == 0:
            return None
        elif len(words) <= 3:
            wl = words
        else:
            wl = words[:3]
        for key in wl:
            if not key in self.__code_dict:
                return None
        return wl

    def address_to_code(self, address):
        wl = self.address_to_list(address)
        if wl is None:
            return None
        return self.__code_dict[wl[-1]]

    def code_to_address(self, code):
        return self.__c2a(code, [])

if __name__ == "__main__":
    ac = AddressCode()
    ac.code_to_address(370102)
    ac.address_to_list('山东省济南市历下区')
    ac.address_to_code('山东省历下区')

行政区域对照表.csv 下载地址

Github

https://github.com/kamino-space/CnAreaNum

发表评论

电子邮件地址不会被公开。 必填项已用*标注