import json
from configparser import ConfigParser
# test.json
'''
[{"name": "apple", "price": "10", "amount": "3"},
{"name": "tesla", "price": "100000", "amount": "1"},
{"name": "mac", "price": "3000", "amount": "2"},
{"name": "lenovo", "price": "30000", "amount": "3"},
{"name": "chicken", "price": "10", "amount": "3"}]
'''
# mysql.ini
'''
[DEFAULT]
a = test
[mysql]
default-character-set = utf8
[mysqld]
test1 = 200
datadir = /dbserver/data
port = 33060
character-set-server = utf8
sql_mode = NO_ENGINE_SUBSTRITUTION,STRICT_TRANS_TABLES
c = True
[test]
test1 = 100
'''
# sun.csv
'''
name,price,amount
apple,10,3
tesla,100000,1
mac,3000,2
lenovo,30000,3
chicken,10,3
'''
# file:json to csv
def json2csv(json_path, csv_path):
with open(json_path, 'r', encoding='utf-8') as f1:
with open(csv_path, 'w', newline='') as f2:
ls = json.load(f1)
data = [list(ls[0].keys())]
for item in ls:
python实现json、ini、csv格式的文件转换
最新推荐文章于 2024-08-15 17:43:43 发布