我想使用python中的mysql-connector
和json
库将一个带有一个表的mysql数据库导出到json中。
我尝试的内容:
import mysql-connector, json
cursor.execute("SELECT * FROM records")
data=cursor.fetchall()
for e in data:
print(json.dumps(data, sort_keys=True, indent=4, separators=(',', ': ')))
回溯错误:
Traceback (most recent call last):
File "bsentor.py", line 238, in <module>
print(json.dumps(data))
File "C:\Users\Justo Gregorio\AppData\Local\Programs\Python\Python37\lib\json\__init__.py", line 231, in dumps
return _default_encoder.encode(obj)
File "C:\Users\Justo Gregorio\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "C:\Users\Justo Gregorio\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 257, in iterencode
return _iterencode(o, 0)
File "C:\Users\Justo Gregorio\AppData\Local\Programs\Python\Python37\lib\json\encoder.py", line 179, in default
raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type date is not JSON serializable
2条答案
按热度按时间ev7lccsx1#
如果您有无法序列化为JSON的对象,则可以在
json.dumps
上使用default=str
zbdgwd5y2#
我将表的排序规则更新为utf8mb3_unicode_ci
我用这个python代码得到了json结果