Sam*_*Roy -3 python json python-2.7
在json.dump方法(python 2.7.1)中,输出的默认分隔符为(','和':').我想删除逗号和冒号,以便我的输出简单地用空格分隔.
我还想删除开口和闭合括号.是否有任何特定的分隔符或字符串格式属性允许我这样做或有任何其他解决方案?
例如申请后
open(foutput,'a')为f1:json.dump(newdict,f1,sort_keys = True,indent = 4)
我得到输出为:
{
"0.671962000": 51.61292129099999,
"0.696699155": 51.61242420999999,
"0.721436310": 51.610724798999996,
"0.746173465": 51.60536924799999,
"0.770910620": 51.58964636499999,
"0.795647775": 51.543248571999996,
"0.820384930": 51.381941735,
}
Run Code Online (Sandbox Code Playgroud)
但我想要下面的类型输出而不是:
0.671962000 -28.875564044
0.696699155 -28.876061125
0.721436310 -28.877760536
0.746173465 -28.883116087
0.770910620 -28.898838970
Run Code Online (Sandbox Code Playgroud)
请注意我只想在python中使用它.提前致谢!
您没有生成JSON,因此请勿使用JSON模块.您正在生成CSV数据,并使用空格作为分隔符.使用该csv模块,或使用简单的字符串格式.
使用csv模块:
import csv
with open(foutput, 'a', newline='') as f1:
writer = csv.writer(f1, delimiter=' ')
writer.writerows(sorted(newdict.items()))
Run Code Online (Sandbox Code Playgroud)
或者只是使用字符串格式:
with open(foutput, 'a') as f1:
for key, value in sorted(newdict.items()):
f1.write('{} {}\n'.format(key, value)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
455 次 |
| 最近记录: |