我想使用python将我的json文件转换为csv文件。我写了一个脚本,但脚本无法正常工作。我的json模板看起来像
{
"-K6v8Ht6nXCjaO_ApNGr" : {
"key1" : 0,
"key2" : false,
"key3" : 0,
"key4" : 0,
"key5" : "01/01/2016 04:04",
"key" : "-K6v8Ht6nXCjaO_ApNGr",
"key6" : "01/01/2016 04:04",
"key7" : 23,
"key8" : 0,
"key9" : {
"date" : 1,
"day" : 5,
"hours" : 4,
"minutes" : 4,
"month" : 0,
"seconds" : 56,
"time" : 1451617496647,
"timezoneOffset" : -60,
"year" : 116
},
"triedConnector" : {
"OPENSL" : -1,
"SAPA" : -1,
"USB" : -1,
"USB_7" : -1
},......
我的脚本(V3.x):
import json,csv,sys
# -*- coding:utf-8 -*-
fileInput = sys.argv[1]
fileOutput = sys.argv[2]
inputFile = open(fileInput,'r') #open json file
outputFile = open(fileOutput, 'w+') #load csv file
data = json.load(inputFile) #load json content
inputFile.close() #close the input file
csv_file = csv.writer(outputFile)
csv_file.writerow(["header 0", "header 1", "header 2", "header 3", "header 4"]) #headers
for value in data.values():
for v in value.values():
csv_file.writerow([unicode(v).encode('utf-8')])
for value in data.values():
for v in value.values():
for z in v.values():
csv_file.writerow([unicode(z).encode('utf-8')])
outputFile.close()#close the output file
在这个脚本中,第一个for循环应该像
一样打印-K6v8Ht6nXCjaO_ApNGr
-K6v8Ht6nXCjaO_ApNGr
-K6v8Ht6nXCjaO_ApNGr
并且它完美地工作但不幸的是我的第二个for循环将此输出提供给我的csv文件
-KjR-JvTm3BKVkYWoyTV
-Kj5ZdfgX8lLLi-htqPU
-KkC4t0XQoJDViNqt18n
False
"{u'seconds': 18, u'year': 117, u'month': 4, u'hours': 8, u'time': 1494223818226L, u'date': 8, u'minutes': 10, u'day': 1, u'timezoneOffset': -120}"
0
08/05/2017 08:10
"{u'OPENSL': -1, u'USB_7': -1, u'SAPA': -1, u'USB': -1}"
08/05/2017 08:10
-KjafVrmb_wuwb36xNq0
0
23
0
0
我想要一个水平输出,这也是我的嵌套对象的第三个循环但是当我尝试运行我的脚本进行最后一个循环时我得到了这个错误:
属性错误:' bool'对象没有属性'值'。
我的问题很简单,我需要像这样的水平输出
K6v8Ht6nXCjaO_ApNGr 0,false,0,0,01 / 01/2016 04:04,-K6v8Ht6nXCjaO_ApNGr K6v8Ht6nXCjaO_ApNGr 0,false,0,0,01 / 01/2016 04:04,-K6v8Ht6nXCjaO_ApNGr
所以任何人都可以帮助我理解并解决这些问题。谢谢
答案 0 :(得分:2)
除了csv字段的顺序不同之外,此代码似乎可以执行您想要的操作。通常这没关系。我还尝试让代码解释它正在做什么。
json是您在问题中提供的三个副本,稍微修改以便正确编译,主键已更改。
json_txt = '''\
{
"-K6v8Ht6nXCjaO_ApNGr" : {
"key1" : 0, "key2" : false, "key3" : 0, "key4" : 0, "key5" : "01/01/2016 04:04",
"key" : "-K6v8Ht6nXCjaO_ApNGr", "key6" : "01/01/2016 04:04", "key7" : 23,
"key8" : 0,
"key9" : { "date" : 1, "day" : 5, "hours" : 4, "minutes" : 4, "month" : 0,
"seconds" : 56, "time" : 1451617496647, "timezoneOffset" : -60, "year" : 116},
"triedConnector" : { "OPENSL" : -1, "SAPA" : -1, "USB" : -1, "USB_7" : -1}
},
"-K6v8Ht6nXCjaO_2" : {
"key1" : 0, "key2" : false, "key3" : 0, "key4" : 0, "key5" : "01/01/2016 04:04",
"key" : "-K6v8Ht6nXCjaO_ApNGr", "key6" : "01/01/2016 04:04", "key7" : 23,
"key8" : 0,
"key9" : { "date" : 1, "day" : 5, "hours" : 4, "minutes" : 4, "month" : 0,
"seconds" : 56, "time" : 1451617496647, "timezoneOffset" : -60, "year" : 116},
"triedConnector" : { "OPENSL" : -1, "SAPA" : -1, "USB" : -1, "USB_7" : -1}
},
"-K6v8Ht6nXCjaO_3" : {
"key1" : 0, "key2" : false, "key3" : 0, "key4" : 0, "key5" : "01/01/2016 04:04",
"key" : "-K6v8Ht6nXCjaO_ApNGr", "key6" : "01/01/2016 04:04", "key7" : 23,
"key8" : 0,
"key9" : { "date" : 1, "day" : 5, "hours" : 4, "minutes" : 4, "month" : 0,
"seconds" : 56, "time" : 1451617496647, "timezoneOffset" : -60, "year" : 116},
"triedConnector" : { "OPENSL" : -1, "SAPA" : -1, "USB" : -1, "USB_7" : -1}
}
}'''
import json
import csv
data = json.loads(json_txt)
print ('data keys -->', data.keys())
print ('one of these keys (arbitrary) -->', list(data.keys())[0])
print ('keys for one object in data -->', list(data[list(data.keys())[0]].keys()))
key9_fields = data[list(data.keys())[0]]['key9'].keys()
print ('keys in key9 -->', key9_fields)
print ('Now writing csv ...')
the_fieldnames = list(data[list(data.keys())[0]].keys())
# remove triedConnector and key9
the_fieldnames.remove('triedConnector')
the_fieldnames.remove('key9')
# add items from key9
the_fieldnames.extend(key9_fields)
print (the_fieldnames)
with open('tpbafk_csv.csv', 'w', newline='') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=the_fieldnames)
writer.writeheader()
for key in data.keys():
row = {}
for field_key in the_fieldnames:
if field_key in key9_fields:
row[field_key] = data[key]['key9'][field_key]
else:
row[field_key] = data[key][field_key]
writer.writerow(row)
打印在stdout上的结果:
data keys --> dict_keys(['-K6v8Ht6nXCjaO_2', '-K6v8Ht6nXCjaO_3', '-K6v8Ht6nXCjaO_ApNGr'])
one of these keys (arbitrary) --> -K6v8Ht6nXCjaO_2
keys for one object in data --> ['key6', 'key8', 'triedConnector', 'key2', 'key7', 'key4', 'key', 'key5', 'key9', 'key3', 'key1']
keys in key9 --> dict_keys(['timezoneOffset', 'month', 'year', 'hours', 'time', 'day', 'seconds', 'date', 'minutes'])
Now writing csv ...
['key6', 'key8', 'key2', 'key7', 'key4', 'key', 'key5', 'key3', 'key1', 'timezoneOffset', 'month', 'year', 'hours', 'time', 'day', 'seconds', 'date', 'minutes']
csv文件:
key6,key8,key2,key7,key4,key,key5,key3,key1,timezoneOffset,month,year,hours,time,day,seconds,date,minutes
01/01/2016 04:04,0,False,23,0,-K6v8Ht6nXCjaO_ApNGr,01/01/2016 04:04,0,0,-60,0,116,4,1451617496647,5,56,1,4
01/01/2016 04:04,0,False,23,0,-K6v8Ht6nXCjaO_ApNGr,01/01/2016 04:04,0,0,-60,0,116,4,1451617496647,5,56,1,4
01/01/2016 04:04,0,False,23,0,-K6v8Ht6nXCjaO_ApNGr,01/01/2016 04:04,0,0,-60,0,116,4,1451617496647,5,56,1,4
答案 1 :(得分:0)
如果csvfile是文件对象,则应使用newline =''
打开它
只需修改此行:
outputFile = open(fileOutput, 'wb+', newline='') #load csv file
答案 2 :(得分:0)
我认为你的json会是这样的,所以我根据下面写了这个脚本:
data = {
"-K6v8Ht6nXCjaO_ApNGr" : {
"key1" : 0,
"key2" : False,
"key3" : 0,
"key4" : 0,
"key5" : "01/01/2016 04:04",
"key" : "-K6v8Ht6nXCjaO_ApNGr",
"key6" : "01/01/2016 04:04",
"key7" : 23,
"key8" : 0,
"key9" : {
"date" : 1,
"day" : 5,
"hours" : 4,
"minutes" : 4,
"month" : 0,
"seconds" : 56,
"time" : 1451617496647,
"timezoneOffset" : -60,
"year" : 116
},
"triedConnector" : {
"OPENSL" : -1,
"SAPA" : -1,
"USB" : -1,
"USB_7" : -1
}}, "-K6v8Ht6nXCjaO_ApNGr1" : {
"key1" : 0,
"key2" : False,
"key3" : 0,
"key4" : 0,
"key5" : "01/01/2016 04:04",
"key" : "-K6v8Ht6nXCjaO_ApNGr",
"key6" : "01/01/2016 04:04",
"key7" : 23,
"key8" : 0,
"key9" : {
"date" : 1,
"day" : 5,
"hours" : 4,
"minutes" : 4,
"month" : 0,
"seconds" : 56,
"time" : 1451617496647,
"timezoneOffset" : -60,
"year" : 116
},
"triedConnector" : {
"OPENSL" : -1,
"SAPA" : -1,
"USB" : -1,
"USB_7" : -1
}}}
代码:
all_keys = ['key']
all_keys.extend('key%d' % i for i in range(1,6))
csv_data = [[data[val][key] for key in all_keys] for val in data.keys()]
import csv
with open('horizontal.csv','w+') as outfile:
write = csv.writer(outfile)
write.writerows(csv_data)
输出:
-K6v8Ht6nXCjaO_ApNGr,False,0,0,01/01/2016 04:04
-K6v8Ht6nXCjaO_ApNGr,False,0,0,01/01/2016 04:04