在使用pandas.io.json.json_normalize时如何将元字段设置为可选?

时间:2019-05-24 23:10:14

标签: python json pandas

在使用json_normalize时遇到了非常典型的键错误。我已经设置了errors =“ ignore”。不幸的是,这仅适用于记录值,而不适用于元值。有没有人找到使meta字段可选的有效方法?我的目标是使用客户端关心的每个可能的字段查询json文件,并为缺少的内容返回NaN。

我的示例用法:

df = json_normalize(data, ['invoice-lines', 'account-allocations'], parent_fields , meta_prefix='invoice_', record_prefix='alloc_', errors='ignore')

我的示例错误:

  

KeyError:“标头税率”

我的“父字段”,元字段的长列表:

parent_fields = [
"id",
"created-at",
"status",
"internal-note",
"invoice-date",
"invoice-number",
"line-level-taxation",
"supplier-note",
"header-tax-rate",
"header-tax-code",
"paid",
"payment-date",
"payment-notes",
"exported",
"account-type-id",
"account-type-name",
"payment-term",
"handling-amount",
"misc-amount",
"shipping-amount",
"total",
"currency",
"accounting-total-amount",
"accounting-total-currency",
"supplier-id",
"supplier-name",
"supplier-number",
"created-by-email",
"created-by-employee-number",
"created-by-login",
"updated-by-email",
"updated-by-employee-number",
"updated-by-login",
"attachment-texts",
"comments",
"remit-to-address.code",
"remit-to-address.attention",
"remit-to-address.street1",
"remit-to-address.street2",
"remit-to-address.city",
"remit-to-address.state",
"remit-to-address.postal-code",
"remit-to-address.country-code",
"remit-to-address.country-name",
"remit-to-address.name",
"remit-to-address.vat-number",
"remit-to-address.vat-country-code",
"remit-to-address.vat-country-name",
"supplier-vat-number",
"supplier-vat-country-code",
"document-type",
"original-invoice-number",
"original-invoice-date",
"bill-to-address.legal-entity-name",
"bill-to-address.attention",
"bill-to-address.street1",
"bill-to-address.street2",
"bill-to-address.city",
"bill-to-address.state",
"bill-to-address.postal-code",
"bill-to-address.country-code",
"bill-to-address.country-name",
"bill-to-address.name",
"ship-to-address.attention",
"ship-to-address.street1",
"ship-to-address.street2",
"ship-to-address.city",
"ship-to-address.state",
"ship-to-address.postal-code",
"ship-to-address.country-code",
"ship-to-address.country-name",
"ship-to-address.name",
"supplier-remit-to.code",
"supplier-remit-to.supplier-name",
"supplier-remit-to.street1",
"supplier-remit-to.street2",
"supplier-remit-to.city",
"supplier-remit-to.state",
"supplier-remit-to.postal-code",
"supplier-remit-to.country-code",
"supplier-remit-to.country-name",
"Fiscal Rep. Name",
"Fiscal Rep. VAT ID",
"Fiscal Rep. Address Code",
"Fiscal Rep. Address Street 1",
"Fiscal Rep. Address Street 2",
"Fiscal Rep. Address City",
"Fiscal Rep. Address State",
"Fiscal Rep. Address Postal Code",
"Fiscal Rep. Address Country Code",
"discount_due_date",
"net_due_date",
"discount_amount",
"late-payment-penalties",
"margin-scheme",
"cash-accounting-scheme-reference",
"exchange-rate",
"early-payment-provisions",
"pre-payment-date",
"reverse-charge-reference",
"discount-percent",
"credit-note-differences-with-original-invoice",
"customs-declaration-number",
"customs-office",
"customs-declaration-date",
"payment-order-reference",
"advance-payment-received-amount",
"series",
"folio-number",
"use-of-invoice",
"form-of-payment",
"type-of-receipt",
"payment-method",
"issuance-place",
"confirmation",
"withholding-tax-override",
"type-of-relationship",
"gross-total",
"registered-company-registration-number",
"registered-company-place-of-registration",
"registered-company-type",
"registered-company-managing-directors",
"credit-reason",
"registered-company-permit-number",
"registered-company-permit-date",
"registered-company-liquidator-name",
"registered-company-share-capital",
"Tax Nature",
"registered-company-unique-shareholder",
"registered-company-liable-company",
"registered-company-tax-regime",
"registered-company-legal-status",
"QST Registration Number",
"Authorised Person",
"Authorised Person Address",
"Authorised Person VAT ID",
"Date of Registration",
"Sole Registration Code",
"origin-currency-net",
"taxes-in-origin-country-currency",
"origin-currency-gross",
"self-billing-reference",
"registered-company-legal-type",
"registered-company-registered-seat",
"registered-company-chairman-of-the-board",
"registered-company-court-of-registration",
"registered-company-liquidation-remark",
"registered-company-commercial-register-number",
"Disputed Invoice Number",
"Channel",
"Taxable Amount",
"invoice-from-code",
"invoice-from-supplier-name",
"invoice-from-street1",
"invoice-from-street2",
"invoice-from-city",
"invoice-from-state",
"invoice-from-postal-code",
"invoice-from-country-code",
"invoice-from-country-name",
"ship-from-code",
"ship-from-supplier-name",
"ship-from-street1",
"ship-from-street2",
"ship-from-city",
"ship-from-state",
"ship-from-postal-code",
"ship-from-country-code",
"ship-from-country-name",
"supplier-tax-registration-number",
"supplier-tax-registration-country-code",
"supplier-tax-registration-country-name",
"supplier-tax-registration-local",
"buyer-tax-registration-number",
"buyer-tax-registration-country-code",
"buyer-tax-registration-country-name",
"buyer-tax-registration-local",
['invoice_lines', 'id']] 

0 个答案:

没有答案