Python 将列表写入 JSON 文件
可以使用 Python 的 json 模块中的 dumps() 函数将 Python 列表转换为 JSON 格式的字符串,并将该字符串写入 JSON 文件。
示例代码:
import json
my_list = [1, 2, 3, 4, 5]
# 将列表转换为 JSON 格式字符串
json_str = json.dumps(my_list)
# 将 JSON 字符串写入文件
with open('data.json', 'w') as f:
f.write(json_str)
运行该代码后,会在当前目录下创建一个名为 data.json 的文件,其中包含了转换后的 JSON 格式字符串。
原文地址: https://www.cveoy.top/t/topic/n7oz 著作权归作者所有。请勿转载和采集!