中国四级城市联动数据,包含港澳台,内含json , sql , python 脚本

python脚本:

复制代码
import json
from pypinyin import lazy_pinyin, Style

def get_pinyin(text):
    full = ''.join(lazy_pinyin(text, style=Style.TONE3))
    short = ''.join([p[0] for p in lazy_pinyin(text, style=Style.TONE3)])
    return full, short

def escape_sql(s):
    return s.replace("'", "''")

def main():
    with open('pcas-code.json', 'r', encoding='utf-8') as f:
        provinces = json.load(f)

    records = []
    id_counter = 1
    province_id_map = {}
    city_id_map = {}
    district_id_map = {}

    # === 构建所有记录(四级)===
    for province in provinces:
        p_code = province['code']
        p_name = province['name']
        pf, ps = get_pinyin(p_name)
        records.append({
            'id': id_counter,
            'name': p_name,
            'province': p_name,
            'code': p_code,
            'level': 1,
            'parent_id': 0,
            'pinyin': pf,
            'short_pinyin': ps
        })
        province_id_map[p_code] = id_counter
        id_counter += 1

        for city in province.get('children', []):
            c_code = city['code']
            c_name = city['name']
            cf, cs = get_pinyin(c_name)
            records.append({
                'id': id_counter,
                'name': c_name,
                'province': p_name,
                'code': c_code,
                'level': 2,
                'parent_id': province_id_map[p_code],
                'pinyin': cf,
                'short_pinyin': cs
            })
            city_id_map[c_code] = id_counter
            id_counter += 1

            for district in city.get('children', []):
                d_code = district['code']
                d_name = district['name']
                df, ds = get_pinyin(d_name)
                records.append({
                    'id': id_counter,
                    'name': d_name,
                    'province': p_name,
                    'code': d_code,
                    'level': 3,
                    'parent_id': city_id_map[c_code],
                    'pinyin': df,
                    'short_pinyin': ds
                })
                district_id_map[d_code] = id_counter
                id_counter += 1

                for town in district.get('children', []):
                    t_code = town['code']
                    t_name = town['name']
                    tf, ts = get_pinyin(t_name)
                    records.append({
                        'id': id_counter,
                        'name': t_name,
                        'province': p_name,
                        'code': t_code,
                        'level': 4,
                        'parent_id': district_id_map[d_code],
                        'pinyin': tf,
                        'short_pinyin': ts
                    })
                    id_counter += 1

    print(f"📊 共生成 {len(records)} 条记录(省+市+区县+乡镇)")

    # === 分批写入 SQL ===
    batch_size = 1000
    total_batches = (len(records) + batch_size - 1) // batch_size

    with open('insert_city_batch_1000.sql', 'w', encoding='utf-8') as f:
        f.write("-- 四级行政区划数据(省/市/区县/乡镇)\n")
        f.write("-- 每批 1000 条,自动分批插入\n")
        f.write("SET foreign_key_checks = 0;\n")
        f.write("SET sql_log_bin = 0; -- 可选:跳过 binlog(如需)\n\n")

        for i in range(0, len(records), batch_size):
            batch = records[i:i + batch_size]
            f.write("INSERT INTO `city` (`id`, `name`, `province`, `code`, `level`, `parent_id`, `pinyin`, `short_pinyin`, `sort`, `del_flag`, `create_time`, `update_time`) VALUES\n")
            lines = []
            for r in batch:
                parent_str = 'NULL' if r['parent_id'] is None else str(r['parent_id'])
                name = escape_sql(r['name'])
                province = escape_sql(r['province'])
                line = f"({r['id']}, '{name}', '{province}', '{r['code']}', {r['level']}, {parent_str}, '{r['pinyin']}', '{r['short_pinyin']}', 0, '0', NOW(), NOW())"
                lines.append(line)
            f.write(",\n".join(lines))
            f.write(";\n\n")

        f.write("SET foreign_key_checks = 1;\n")
        f.write("-- ✅ 导入完成\n")

    print(f"✅ 已生成 {total_batches} 个批次")
    print("📄 输出文件:insert_city_batch_1000.sql")

if __name__ == '__main__':
    main()
相关推荐
水獭比特20 分钟前
localhost 不是安全边界:给 Agent Web 入口补上四层门禁
人工智能·python
Generalzy22 分钟前
Whisper + VAD + TTS:一套完整的 Python 本地语音处理流水线
python·whisper·语音识别
qpsj22 分钟前
让 LLM 控制 AutoCAD/ZWCAD:COM 自动化 + MCP 封装
python·llm
赟爸27 分钟前
直播切片素材杂乱不好复用,易元AI要怎么处理
大数据·人工智能·python
怪奇云呼军1 小时前
从声音特征到 CRM 回流:闪电智能 Voice Agent 沟通策略自适应系统 v1 实战
android·人工智能·python·音视频·语音识别
jufeng13071 小时前
【系列:手搓自主 AI Agent:Hermes 架构原理剖析 · 第 6 篇】
python·ai agent·记忆系统
kevinnett2 小时前
别再把模型地址写死了:用 Python 设计一个可切换的 LLM 调用层
python
天天进步20152 小时前
Python全栈项目--协同办公平台
开发语言·python
GEOshijie1233 小时前
GEO服务商算法适配承诺怎么验收?48小时响应的合同化考核方案
人工智能·python
卷无止境3 小时前
FastAPI 前端托管全攻略:从静态文件到大型全栈项目架构
后端·python