python json转json字符串时候,中文被转义解决

核心

json.dumps(jsonData, ensure_ascii=False)
import pymysql
import json

conn = pymysql.connect(
    host='127.0.0.1',
    port=3306,
    user='root',
    password='root',
    db='test'
)

cursor = conn.cursor()

sql = "select * from users"

cursor.execute(sql)

# row_1 = cursor.fetchone()
row_2 = cursor.fetchmany(3)

# print(row_1)
# print(row_2)
# print(list(row_2))

jsonData = []

for row in row_2:
    obj = {}

    obj['id'] = row[0]
    # obj['createTime'] = row[1]
    # obj['updateTime'] = row[2]
    obj['name'] = row[4]
    obj['phone'] = row[5]

    jsonData.append(obj)
    # print(row)

print(jsonData)

# json转字符串中文转字符
print(json.dumps(jsonData, ensure_ascii=False))

with open('./basedata.json', 'w') as f:
    f.write(json.dumps(jsonData, ensure_ascii=False))

conn.commit()
cursor.close()
conn.close()

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 成长之路 设计师:Amelia_0503 返回首页