如何使用writer和unicodecsv添加标题行

时间:2017-10-11 12:46:58

标签: python json

我有以下JSON文件 - test.json(出于安全原因更改了名称,密钥和地址)

   [  
   {  
      "accountMode":"Live",
      "acquirer":"TEST",
      "acquirerConstraints":{  
         "cardTypes":[  
            "MASTERCARD",
            "MAESTRO",
            "VISA"
         ],
         "cvcRegexp":"^[0-9]{3}$",
         "cvcRequired":true,
         "maxAmount":500000,
         "minAmount":50
      },
      "acquirerDetails":{  
         "TEST":"Studio",
         "ERROR_LIST":[  

         ],
         "MERCHANT_CODE":"218331",
         "VALID":true,
         "_mId":"T712484",
         "_status":"INPROCESS",
         "email":"test7@gmail.com",
         "name":"Studio",
         "valid":true
      },
      "acquirerValidations":null,
      "allowedCurrencies":[  
         "EUR",
         "USD",
         "GBP"
      ],
      "apiKeyPairs":[  
         {  
            "accountMode":"Live",
            "label":"Virtual Terminal",
            "publishableKey":"niunibiubniunijknkjknj",
            "source":"VIRTUAL_TERMINAL"
         },
         {  
            "accountMode":"Live",
            "label":"Default",
            "publishableKey":"iiuhiuhiu",
            "source":"ECOMMERCE"
         }
      ],
      "appLogoUrl":null,
      "applicationId":"541d75e0-7db8b343a31f",
      "authorizationCode":"",
      "closedDate":null,
      "closureReason":null,
      "declineAvsAddressFailure":false,
      "declineAvsZipFailure":false,
      "declineCvcFailure":false,
      "defaultCurrency":"EUR",
      "descriptor":null,
      "email":"test1@gmail.com",
      "id":"ddddeff",
      "invitationCode":null,
      "locale":"en_IE",
      "merchantApplication":{  
         "accountNumber":null,
         "acquirer":"TEST",
         "annualAmount":null,
         "annualVolume":null,
         "applicationType":"APPROVAL",
         "bankName":"UNKNOWN",
         "brand":null,
         "businessAddress":"54 My St, 1",
         "businessAddress2":null,
         "businessCity":"Abbey",
         "businessCountry":"IRL",
         "businessPhone":null,
         "businessState":"DUBLIN",
         "businessZip":null,
         "data":null,
         "email":"test@gmail.com",
         "escalationPhone":null,
         "fax":null,
         "legalName":"UAB \"Studio\"",
         "maxTransactionAmount":null,
         "mccCode":"5712",
         "merchantPromotionCode":null,
         "mobile":null,
         "monthlyAmount":null,
         "monthlyVolume":null,
         "ownerFirstName":"tlana",
         "ownerLastName":"nava",
         "phone":"37647",
         "GuideAccepted":null,
         "privacyAccepted":true,
         "privacyVersion":"1a",
         "referenceId":"9104d65i08d071",
         "routingNumber":null,
         "singleTransactionAmount":null,
         "statementName":"UAB \"Studio\"",
         "taxId":null,
         "termsAccepted":true,
         "termsVersion":"1a",
         "url":"http://www.design.lt"
      },
      "merchantId":"12484",
      "merchantPromotionCode":null,
      "mposEnabled":true,
      "name":"Studio",
      "netonfiguration":null,
      "onboardedDate":1505513232485,
      "onboardingMethod":null,
      "onboardingStatus":"INPROCESS",
      "partner":null,
      "saqCompliant":false,
      "saqExpires":null,
      "settings":[  
         {  
            "key":"MERCHANT_DETAILS",
            "value":"{\"zip\":\"Wicklow\",\"phone\":\"342647\",\"email\":\"suppoor@outlook.com\",\"address\":\"Bck 6\",\"state\":\"Ireland\",\"addressLine2\":\"Unit 8, Bl Par\",\"city\":\"Wicklow\"}"
         },
         {  
            "key":"VAT_NUMBER",
            "value":"/evzaqen/"
         }
      ],
      "timezone":"Europe/Dublin",
      "tinStatus":null
   },
   {  
      "accountMode":"Live",
      "acquirer":"TEST",
      "acquirerConstraints":{  
         "cardTypes":[  
            "MASTERCARD",
            "MAESTRO",
            "VISA"
         ],
         "cvcRegexp":"^[0-9]{3}$",
         "cvcRequired":true,
         "maxAmount":500000,
         "minAmount":50
      },
      "acquirerDetails":{  
         "TEST":"test",
         "ERROR_LIST":[  

         ],
         "MERCHANT_CODE":"594920",
         "MID_ASSIGNED":true,
         "VALID":true,
         "_mId":"103558",
         "_status":"APPROVED",
         "acquiringMid":"1036598",
         "descriptor":"test 8885551212",
         "email":"test@gmail.com",
         "gatewayMid":"SIMP337",
         "id":"SIMP337",
         "level4Mid":"76576576",
         "name":"test",
         "status":"APPROVED",
         "transactionCurrency":"USD;EUR;GBP",
         "valid":true,
         "paymentGatewayKey":"ytfytfytfyt"
      },
      "acquirerValidations":null,
      "allowedCurrencies":[  
         "EUR",
         "USD",
         "GBP"
      ],
      "apiKeyPairs":[  

      ],
      "appLogoUrl":null,
      "applicationId":"949bdde5-07-d8d58f4c3d01",
      "authorizationCode":"",
      "closedDate":null,
      "closureReason":null,
      "declineAvsAddressFailure":false,
      "declineAvsZipFailure":false,
      "declineCvcFailure":false,
      "defaultCurrency":"EUR",
      "descriptor":"test85551212",
      "email":"test@gmail.com",
      "id":"9f3a7d7",
      "invitationCode":null,
      "locale":"en_US",
      "merchantApplication":{  
         "accountNumber":null,
         "acquirer":"TEST",
         "annualAmount":null,
         "annualVolume":null,
         "applicationType":"APPROVAL",
         "bankName":"UNKNOWN",
         "brand":null,
         "businessAddress":"123 test",
         "businessAddress2":null,
         "businessCity":"Atlanta",
         "businessCountry":"IRL",
         "businessPhone":null,
         "businessState":"CARLOW",
         "businessZip":null,
         "data":null,
         "email":"test@gmail.com",
         "escalationPhone":null,
         "fax":null,
         "legalName":"stest",
         "maxTransactionAmount":null,
         "mccCode":"521",
         "merchantPromotionCode":null,
         "mobile":null,
         "monthlyAmount":null,
         "monthlyVolume":null,
         "ownerFirstName":"moto",
         "ownerLastName":"test",
         "phone":"3141212",
         "GuideAccepted":null,
         "privacyAccepted":true,
         "privacyVersion":"1a",
         "referenceId":"2920",
         "routingNumber":null,
         "singleTransactionAmount":null,
         "statementName":"test",
         "taxId":null,
         "termsAccepted":true,
         "termsVersion":"1a",
         "url":null
      },
      "merchantId":"1036558",
      "merchantPromotionCode":null,
      "mposEnabled":true,
      "name":"test",
      "netonfiguration":null,
      "onboardedDate":1456846054925,
      "onboardingMethod":null,
      "onboardingStatus":"CLOSED",
      "partner":null,
      "saqCompliant":false,
      "saqExpires":null,
      "settings":[  

      ],
      "timezone":"Europe/Dublin",
      "tinStatus":"InCompliance"
   }
]

我想处理此文件并获取一些信息并使用它填充CSV文件。为此,我使用以下内容:

import unicodecsv
import json

json_data = open("test.json")
data = json.load(json_data)

f = unicodecsv.writer(open("results.csv","wb+"))
for entry in data:
    if "merchantApplication" in entry:
        ma = entry["merchantApplication"]
        if "email" in ma:
            f.writerow([ma["ownerFirstName"],ma["ownerLastName"],ma["email"],ma["legalName"],ma["businessAddress"],ma["businessAddress2"],ma["businessCity"],ma["businessCountry"],ma["businessState"],ma["businessZip"],ma["phone"],ma["mobile"]])

json_data.close()

此工作正常但不打印列上方的标题。如何添加标题?我使用的是Python 2.7.10

1 个答案:

答案 0 :(得分:2)

  

如何添加标题?

很简单,在f.writerow((<your>,<headers>,<here>))循环之前调用for