Python脚本生成一个JSON文件(data.json)。我试图从JSON值中删除空格,但不知道如何做到这一点。我曾试图在json.dump函数中指定separators标志。https://docs.python.org/3/library/json.html#json.dump。但没有运气。
有什么提示/想法吗?
import csv
import json
def csv_to_json(csvFilePath, jsonFilePath):
jsonArray = []
with open(csvFilePath, encoding='utf-8-sig') as csvf:
#load csv file data using csv library's dictionary reader
csvReader = csv.DictReader(csvf)
#convert each csv row into python dict
for row in csvReader:
#add this python dict to json array
jsonArray.append(row)
#convert python jsonArray to JSON String and write to file
with open(jsonFilePath, 'w', encoding='utf-8') as jsonf:
jsonString = json.dumps(jsonArray, indent=4, ensure_ascii=False)
jsonf.write(jsonString)
csvFilePath = r'input.csv'
jsonFilePath = r'data.json'
csv_to_json(csvFilePath, jsonFilePath)
data.json
{
"Primary_Permission_All": "True ",
"Primary_FirstName": "Donald",
"Primary_LastName": "Duck",
"Primary_Email": "donald@duck.com",
"Primary_MobilePhone": "11223344 ",
"Primary_Permission_Letter": "True ",
"Primary_Permission_Newsletter": "True ",
"Primary_Permission_Phone": "True ",
"Primary_Permission_SMS": "True ",
"Primary_WorkPhone": "11223344 "
},
1条答案
按热度按时间pepwfjgg1#
这是在ChatGPT的帮助下解决的:)