1
hereからネストされたJSONデータを解析しています。このファイル内の一部のファイルには、複数のファイルが関連付けられています。各ファイルに関連するすべての委員会が必要です。私は確信していませんが、それはそれぞれcommittee_id
の新しい行を書くことを意味すると思います。私のコードは次のとおりです。パンダ:JSONファイルからネストされたデータを取得する
import os.path
import csv
import json
path = '/home/jayaramdas/anaconda3/Thesis/govtrack/bills109/hr'
dirs = os.listdir(path)
outputfile = open('df/h109_s_b', 'w', newline='')
outputwriter = csv.writer(outputfile)
for dir in dirs:
with open(path + "/" + dir + "/data.json", "r") as f:
data = json.load(f)
a = data['introduced_at']
b = data['bill_id']
c = data['sponsor']['thomas_id']
d = data['sponsor']['state']
e = data['sponsor']['name']
f = data['sponsor']['type']
i = data['subjects_top_term']
j = data['official_title']
if data['committees']:
g = data['committees'][0]['committee_id']
else:
g = "None"
outputwriter.writerow([a, b, c, d, e, f, g, i, j])
outputfile.close()
を私が午前問題は、私のコードのみ記載されている最初のcommittee_id
を収集しているということです。たとえば、ファイルは次のようになります。
bill_iid committee subcommittee introduced at Thomas_id state name
hr145-109 HSPW na "2005-01-4" 73 NY "McHugh, John M."
hr145-109 HSPW 13 "2005-01-4" 73 NY "McHugh, John M."
hr145-109 HSBA na "2005-01-4" 73 NY "McHugh, John M."
hr145-109 HSBA 19 "2005-01-4" 73 NY "McHugh, John M."
:法案は、小委員会に渡されるとき、私も
committee_id
に関連付けられている
subcommittee_id
をしたいので、それは少しトリッキーです
"committees": [
{
"activity": [
"referral",
"in committee"
],
"committee": "House Transportation and Infrastructure",
"committee_id": "HSPW"
},
{
"activity": [
"referral"
],
"committee": "House Transportation and Infrastructure",
"committee_id": "HSPW",
"subcommittee": "Subcommittee on Economic Development, Public Buildings and Emergency Management",
"subcommittee_id": "13"
},
{
"activity": [
"referral",
"in committee"
],
"committee": "House Financial Services",
"committee_id": "HSBA"
},
{
"activity": [
"referral"
],
"committee": "House Financial Services",
"committee_id": "HSBA",
"subcommittee": "Subcommittee on Domestic and International Monetary Policy, Trade, and Technology",
"subcommittee_id": "19"
}
これは、
アイデア?
よりマックス!私は小さな質問があります: 'fn'は何を指しているべきですか?待って、私はそれを持っていると思います。 'fn' =' filename'です。 –
@MichaelPerdue、はい、ファイル名の完全なパスまたは相対パスである必要があります。 – MaxU
私はあなたのコードを1つの例外を除いて適用しました。私は 'NameError:名前 'ujson'が定義されていない 'を取得していたので、jsonをujsonに置き換えました。ただし、1行しか返されません。 fnとして私は '(path +"/"+ dir +" /data.json "、" r ")'を使用していますが、私はおそらくそれを働かせるためにツールを使うことができますが、それが何であるか考えていますか? –