简体   繁体   English

嵌套数据到csv python

[英]Nested data to csv python

I have a file of nested json data. 我有一个嵌套的json数据文件。 I am trying to "get.some_object" and write a csv file with the objects (I think they are called objects: "some_object": "some_value"); 我正在尝试“get.some_object”并用对象编写一个csv文件(我认为它们被称为对象:“some_object”:“some_value”); I would like one row for each group of nested items. 我想为每组嵌套项目添加一行。 This is my code: 这是我的代码:

import csv
import json

path = 'E:/Uni Arbeit/Prof Hayo/Sascha/Bill data/97/bills/hr/hr4242'

outputfile = open('TaxLaw1981.csv', 'w', newline='')                            
outputwriter = csv.writer(outputfile)

with open(path + "/" + "/data.json", "r") as f:
    data = json.load(f)

    for act in data['actions']:
        a = act.get('acted_at')
        b = act.get('text')
        c = act.get('type')

        outputwriter.writerow([a, b, c])
outputfile.close()       

The problem I have is that it only writes the last group of data to csv; 我遇到的问题是它只将最后一组数据写入csv; however when I run 但是当我跑步的时候

with open(path + "/" + "/data.json", "r") as f:
data = json.load(f)

for act in data['actions']:

    a = act.get('acted_at')
    b = act.get('text')
    c = act.get('type')
    print (a) 

all of my "a" values print out. 打印出所有“a”值。

Suggestions? 建议?

You need to flush your outputwriter to write the row to the file, else it will keep on replacing the one in the variable and eventually only write the last value. 您需要刷新输出写入以将行写入文件,否则它将继续替换变量中的行并最终只写入最后一个值。 Writerow only works when you close the file unless you flush the data. 除非您刷新数据,否则Writerow仅在您关闭文件时有效。

for act in data['actions']:
        a = act.get('acted_at')
        b = act.get('text')
        c = act.get('type')
        outputwriter.writerow([a, b, c])
        outputfile.flush()

The code you posted above works 100% with the file you have. 您上面发布的代码与您拥有的文件一起使用100%。

The file (for anyone interested) is available with rsync -avz --delete --delete-excluded --exclude **/text-versions/ govtrack.us::govtrackdata/congress/97/bills/hr/hr4242 . 该文件(对任何感兴趣的人)都可以使用rsync -avz --delete --delete-excluded --exclude **/text-versions/ govtrack.us::govtrackdata/congress/97/bills/hr/hr4242 . .

And the output to the csv file is (omitting some lines in the middle) 并输出到csv文件是(省略中间的一些行)

1981-07-23,Referred to House Committee on Ways and Means.,referral
1981-07-23,"Consideration and Mark-up Session Held by Committee on Ways and Means Prior to Introduction (Jun 10, 81 through Jul 23, 81).",action
1981-07-23,"Hearings Held by House Committee on Ways and Means Prior to Introduction (Feb 24, 25, Mar 3, 4, 5, 24, 25, 26, 27, 30, 31, Apr 1, 2, 3, 7, 81).",action
...
...
...
1981-08-12,Measure Signed in Senate.,action
1981-08-12,Presented to President.,topresident
1981-08-13,Signed by President.,signed
1981-08-13,Became Public Law No: 97-34.,enacted

You should post the full error code you get when you execute (probably due to an encoding error) to let someone understand why your code is failing. 您应该发布执行时获得的完整错误代码(可能是由于编码错误),让某人理解您的代码失败的原因。

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

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