简体   繁体   English

json.dumps添加双引号和反斜杠

[英]json.dumps adding double quotes and backslash

I'm converting a csv file with multiple records to a file with a json array. 我正在将具有多个记录的csv文件转换为具有json数组的文件。 Solution is How to find last line in csv file when using DictReader . 解决方法是使用DictReader时如何在csv文件中查找最后一行 This works fine, except that resulting json in file has backslash (\\) and double-quotes; 除文件中生成的json具有反斜杠(\\)和双引号外,此方法工作正常。 ie it's escaping the strings. 即它在转义字符串。 For example: 例如:

{"Test Name": "Basic Tcp IPerf Test", " \\"Status\\"": "PASS", " \\"Test Start Time\\"": "20190103 08:07:41.662", " \\"Test End Time\\"": "20190103 08:08:44.051", " \\"Throughput\\"": "2095.2746", " \\"Jitter\\"": "", " \\"Packet Loss %\\"": "", " \\"Compute/VM\\"": "SendVm (xxxx)", " \\"Statistics Sampling Start Time\\"": "03 Jan 2019 08:07:44", " \\"Statistics Sampling End Time\\"": "03 Jan 2019 08:08:42", " \\"Min CPU\\"": "0", " \\"Max CPU\\"": "2", " \\"Avg CPU\\"": "1", " \\"Avg Memory (MB)\\" ": "7758"} {“测试名称”:“基本Tcp IPerf测试”,“ \\”状态\\””:“通过”,“ \\”测试开始时间”,“”:“ 20190103 08:07:41.662”,“ \\”测试结束时间\\“”:“ 20190103 08:08:44.051”,“ \\” Throughput \\“”:“ 2095.2746”,“ \\” Jitter \\“”:“”,“ \\”丢包率%\\“”:“”,“ \\“计算/ VM \\”“:” SendVm(xxxx)“,” \\“统计信息采样开始时间\\”“:” 03 Jan 2019 08:07:44“,” \\“统计信息采样结束时间\\”“:” 2019年1月3日08:08:42“,” \\“最小CPU \\”“:” 0“,” \\“最大CPU \\”“”:“ 2”,“ \\”平均CPU \\“”:“ 1”,“ \\“平均内存(MB)\\”“:” 7758“}

Is there a way to write out that json without that escaping? 有没有办法写出json而没有转义? Code: 码:

csvfile = open('fiile.csv','r')
jsonfile = open('file.json','w')

reader = csv.DictReader(csvfile)

jsonfile.write(json.dumps(list(reader)))

It looks like your csv file is formatted in a way that confuses the default csv parser. 看起来您的csv文件的格式设置会混淆默认的csv分析器。 In particular, your columns are separated by both a comma and a space: 特别是,您的列用逗号空格分隔:

           comma
           v
"Test Name", "Status"
            ^
            space

This makes the parser assume that all the text following the comma is part of the value, including the space and the quote mark. 这使解析器假定逗号后面的所有文本都是值的一部分,包括空格和引号。

One possible solution is to specify a value for the skipinitialspace argument when opening the DictReader. 一种可能的解决方案是在打开DictReader时为skipinitialspace参数指定一个值。

import csv
import json

csvfile = open('fiile.csv','r')
jsonfile = open('file.json','w')

reader = csv.DictReader(csvfile, skipinitialspace=True)
jsonfile.write(json.dumps(list(reader)))

Then the parser should understand that the space and quote aren't part of the value. 然后,解析器应了解空格和引号不是值的一部分。 Result: 结果:

[{"Test Name": "Basic Tcp IPerf Test", "Status": "PASS", "Test Start Time": "20190103 08:07:41.662", "Test End Time": "20190103 08:08:44.051", "Throughput": "2095.2746", "Jitter": "", "Packet Loss %": "", "Compute/VM": "SendVm (x.x.x.x)", "Statistics Sampling Start Time": "03 Jan 2019 08:07:44", "Statistics Sampling End Time": "03 Jan 2019 08:08:42", "Min CPU": "0", "Max CPU": "2", "Avg CPU": "1", "Avg Memory (MB)": "7758"}]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM