중첩 된 JSON 오브젝트 파일을 CSV로 변환하려고합니다. I 여기 이 컬럼이다 같은 JSON 샘플JSON에서 CSV 로의 파이썬 변환 문제
{
"total_hosts" : [
{
"TYPE" : "AGENT",
"COUNT" : 6
}
],
"installed" : [
{
"ID" : "admin-4.0",
"VERSION" : 4,
"ADDON_NAME" : "Administration"
},
{
"ID" : "admin-2.0",
"VERSION" : 2,
"ADDON_NAME" : "Administration"
},
{
"ID" : "ch-5.0",
"VERSION" : "5",
"ADDON_NAME" : "Control Host"
}
],
"virtual_machine" : [
{
"COUNT" : 4,
"TYPE" : "VM"
}
TYPE, COUNT, ID, 버전이지만 값을 일부 이러한 값 1 개체가 또는 몇몇 개이므로이 가지는 문제는 각 객체가 아닌 행에 쓰기, 그래서 그 열에 대한 값이없는 경우 빈 공간을 작성하려고합니다.
코드는, 내가 Index out of range
오류가 발생하고 CSV
json_input = open('all.json')
try:
decoded = json.load(json_input)
# tell computer where to put CSV
outfile_path='Path to CSV'
# open it up, the w means we will write to it
writer = csv.writer(open(outfile_path,'w'))
for index in range(len(decoded['installed'])):
row = []
if decoded['total_hosts'][index]['TYPE'] is None:
row.append(str(''))
else:
row.append(str(decoded['total_hosts'][index]['TYPE']))
if decoded['total_hosts'][index]['COUNT'] is None:
row.append(str(''))
else:
row.append(str(decoded['total_hosts'][index]['COUNT']))
writer.writerow(row)
로 쓰기 난 if
에 대한 True
/False
상태를 시도했다.
아무도 도와 줄 수 있습니까?
업데이트 : 예상 출력 : 해당 열의 값이 없을 때
TYPE,COUNT,ID,VERSION,ADDON_NAME,COUNT,TYPE
AGENT,6,admin-4.0,4,Administration,4,VM
, ,admin-2.0,2,Administration, ,
, ,cd-5.0,5,Control Host, ,
그래서 기본적으로 난 빈 공간이 필요합니다.
으론 수정 : OUTPUT :
AGENT,6,,,
, ,admin-4.0,4,Administration
, ,admin-2.0,2,Administration
, ,ch-5.0,5,Control Host
예상 출력 : 업데이트
AGENT,6,admin-4.0,4,Administration
, ,admin-2.0,2,Administration
, ,ch-5.0,5,Control Host
: 나는 심지어 아직 위와 같은 결과를 얻었다
row.append(str(entry.get('TYPE', '')))
row.append(str(entry.get('COUNT', '')))
row.append(str(entry.get('ID', '')))
row.append(str(entry.get('VERSION', '')))
row.append(str(entry.get('ADDON_NAME', '')))
writer.writerow(row)
을 시도했다. :(
귀하의'installed'을하고 : 당신이
(어떤이installed
후 항상 긴, 그리고 가정해야 할 것 파이썬 버전itertools.izip_longest
추가) 2.6 전에 사용 같은 * 길이 *; 당신은'range (len (디코딩 된 [ 'installed']))')를 반복하고 있지만,'decoded [ 'total_hosts']'와'decoded [ '_ hosts']'리스트의 인덱스를 사용합니다 (후자는 아마도 오타입니다). –입력 및 예상 출력의 전체 예제를 포함해야합니다. – njzk2
네 그것은 오타되었습니다 :) 실제로 파일의 모든 요소에 대해 루프를 반복하고 싶습니다. 그러나 그것들은 개별 객체/배열이므로 최대 개수의 요소가있는 배열을 가져 와서 반복합니다. 그래서 IF 조건을 넣습니다. 값이 없으면 공백을 추가해야합니다. 그래서 열 구조를 유지할 수 있습니다. – user3520135