吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 4479|回复: 19
收起左侧

[Python 转载] 全国城市+四级城市地址+邮编+区号+经纬度json版

[复制链接]
kang_alone 发表于 2020-9-5 15:04
【数据来源】
https://www.52pojie.cn/thread-1261184-1-1.html
【转化方式】
[Python] 纯文本查看 复制代码
#!/usr/bin/env python
# coding=utf-8
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import json
# 如果没有pymysql包需先安装
# 安装方法:pip install pymysql
import pymysql


# 连接数据库
conn = pymysql.connect(
            host='localhost',
            user='root',
            passwd='root',
            db='thinkcmf',
            charset='utf8',
            use_unicode=True
        )
# 建立游标cursor
cursor = conn.cursor()

# 执行查
cursor.execute("SELECT * FROM hy_area")
# 查询数据库多条数据
result = cursor.fetchall()

fields = cursor.description

cursor.close()
conn.close()

# 定义字段名的列表
column_list = []
for i in fields:
    # 提取字段名,追加到列表中
    column_list.append(i[0])
# print(column_list)
# ['Id', 'name', 'password', 'birthplace']

# 打开输出结果文件
with open('data/json.txt', 'w+') as f:
    # 一次循环,row代表一行,row以元组的形式显示
    for row in result:
        # 定义Python 字典
        data = {}
        # 将row中的每个元素,追加到字典中。
        for i in range(len(column_list)):
            data[column_list[i]] = row[i]
        # data[column_list[0]] = row[0]
        # # Python字段格式 和json字段格式转换
        # data[column_list[1]] = str(row[1])
        # data[column_list[2]] = str(row[2])
        # data[column_list[3]] = str(row[3])
        # Python的dict --转换成----> json的object
        jsondata = json.dumps(data, ensure_ascii=False)
        # 写入文件
        f.write(jsondata + ',' + '\n')
f.close()

【下载地址】
https://wwa.lanzouj.com/b00tu7pcj
密码:52pj
json格式无法上传,请自行查找美化方式。

免费评分

参与人数 2吾爱币 +2 热心值 +2 收起 理由
51528098 + 1 + 1 我很赞同!
kingtiger + 1 + 1 谢谢@Thanks!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

webboyvc 发表于 2020-9-5 15:12
刚好有用
mcu5i51 发表于 2020-9-5 15:14
 楼主| kang_alone 发表于 2020-9-5 15:41
orb001 发表于 2020-9-5 16:14
json是啥东西呢?
hu007 发表于 2020-9-5 16:26
谢谢分享了
小航 发表于 2020-9-5 16:28
不能解析啊
simida11 发表于 2020-9-5 16:51
有什么用啊
yepp55 发表于 2020-9-5 17:34
这个咋用的,什么用的?
吾爱Joseph 发表于 2020-9-5 18:13
可以写个捷径
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-16 01:56

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表