回答此问题可获得 20 贡献值,回答如果被采纳可获得 50 分。
<p>我试图从JSON字符串中的键中获取一个值,然后将该数据插入到数据帧中。<br/>
但有时该列可能出现在结构中,也可能不出现,因此如果键不存在,则会出现类似<code>'KeyError': 'Company_Name_Abb'</code>的错误</p>
<p>因此,如果我得到这个错误,数据将不会被插入到dataframe,并将<code>pass</code>插入到下一个协定,但这不是我想要的。如果某个键不存在,我想给它设置一个<strong>Null</strong>值,这样它就可以正常插入到数据帧中</p>
<p>因此,如果您注意到下面的JSON字符串,我没有得到<strong>公司名称\u Abb</strong>键</p>
<pre><code>{
"success": "true",
"code": 200,
"messages": null,
"response": {
"templateType": "",
"metadata": {
"AUTO_RENEWAL_PERIOD_MONTHS": "0",
"Reminder_date": "",
"Monetary_Type": "",
"Legal_Entity": "Test Inc.",
"BIZ_DEV_CONTACT": "Goofy",
"Business_Owner": "Minnie Mouse",
"Contract_Status": "Active",
"Cond_exp_terms": "",
"Contract_Category": "General",
"Contract_Term_Period_UoM": "Years",
"Customer_ID_Intel": "",
"Expiry_Date_words": "June 08, 2024",
"Royalty_Max_Value": "",
"TIMEZONE": "US/Pacific - (GMT-8:00)",
"Audit_Section_Num": "",
"Same_as_Contract_Signed_Date": "Enter Date",
"CNDA_Number": "12345",
"Contract_Title": "'TEST AGREEMENT FOR API No 2",
"ConfidentialContract": "No",
"Heirarchy_Status": "Master Agreement",
"RSNDA_Number": "",
"Contract_Signed_On_date": 1623263400000,
"Termination_Section_Num": "",
"Region": "N.A.",
"Contract_Expiration_Date": 1717785000000,
"Historical_Audit_Date": "",
"RSNDA_Date": "",
"PCM_Contact": "",
"License_Technology": "",
"PCM_Obligation": "No",
"MILES_SUMM": "",
"Effective_Date_words": "June 09, 2021",
"Amendments": [],
"Contract_Effective_Date": 1623177000000,
"Folder": "Default",
"RUNDA_Date": "",
"Contract_Owner": "test@test.com",
"Licensed_Tech_Name": "",
"Payment_Terms": "",
"SUBLICENSE": "Test",
"AUTO_RENEW": "Fixed",
"ATTORNEY_NOTES": "",
"Vendor_ID_Intel": "",
"Attorney_Owner": "",
"Monetary_Num": "",
"RUNDA_Number": "",
"Notes": "",
"Contract_Desc": "This is a test",
"ContractualDoc": [],
"Contract_Renewal_Date": "",
"Monetary_Sec_Num": "",
"SearchableContract": "Yes",
"AUTO_RENEWAL_MAXIMUM_NUMBER_OF_RENEWALS": "0",
"AUTO_RENEWAL_NOTICE_DAYS": "0",
"Intel_Product_Family": "",
"CNDA_Date": 1623177000000,
"DEV_PARTNER": "",
"Contract_Term": "3"
},
"contractNumber": "INTEL29017",
"supportingDocuments": [],
"statusCode": "",
},
"responseMap": null
}
</code></pre>
<blockquote>
<p><em><strong>In the example below I just created two variables, but I'm getting all
of the keys from the JSON. And also the JSON above is just one
example, since the Foor Loop retrieves many JSON string as the one
above.</strong></em></p>
</blockquote>
<p>如何获取值(<strong>JSON.text,我正在从API检索)</strong>:</p>
<pre><code>for element in jsoncontracts['response']['rows']:
try:
jsoncontractsdetails = json.loads(JSON.text)
Auto_Renewal_Period_Months, Company_Name_Abb = jsoncontractsdetails['response']['metadata']['AUTO_RENEWAL_PERIOD_MONTHS'], jsoncontractsdetails['response']['metadata']['Company_Name_Abb']
df_contractnumberdetails = pd.DataFrame([[Auto_Renewal_Period_Months, Company_Name_Abb]], columns=['AUTO_RENEWAL_PERIOD_MONTHS', 'Company_Name_Abb'], index=None)
except Exception as e:
print('ContractNumberError: ' + contract)
print(e)
pass
</code></pre>
<p>我已经尝试过排除KeyError,并且能够为它的键分配一个值,但是我无法插入数据帧,因为它正在传递到循环中的下一个协定</p>
<p><strong>更新:</strong></p>
<p>我试图从<code>collections</code>模块中使用<code>defaultdict</code>函数,但在尝试从该函数访问密钥后,仍然得到<code>KeyError: 'Company_Name_Abb'</code></p>
<pre><code>jsoncontractsdetails = defaultdict(lambda: -1, jsoncontractsdetails)
Company_Name_Abb = jsoncontractsdetails['response']['metadata']['Company_Name_Abb']
</code></pre>