将json值打印到Excel中时输入错误。?

时间:2015-12-19 19:54:10

标签: python json excel api

我是python的新手,请帮助我:

我只是想将JSON值打印到excel。

这里使用的代码:

response1 = requests.get(url, headers=headers , verify=False)
    wb = load_workbook('testexcel.xlsx')
    ws = wb.active
    json_data = json.loads(response1.text)
    for row, x in enumerate(json_data):
    ws['A1'] = 'id'
    column_cell = 'A'
    ws[column_cell+str(row+2)] = (x['id'])
    ws['B1'] = 'Closed Time'
    column_cell = 'B'
    ct = x['close_time']
    c = datetime.datetime.fromtimestamp(ct/1000.0).strftime('%Y-%m-%d %H:%M:%S')
    ws[column_cell+str(row+2)] = (c)
    wb.save('testexcel.xlsx')

输入JSON:

{"信誉":2" source_address_ids":[65762,67784,65505,65862,67601,62030,66837,61630,63707,63242,63609,62901,65958 ,62553,62602,62605,62277,66363,62764,64284,63062,64765,67902,63452,65375,65741,68742,63623,64005,69415,59861,60431,65463,66352,69627,66120,60333,64430 ,62968,63766,61863,64693,63589,61126,70587,59288,60358,63567,61537,64115,65827,60592,65199,64586,66410,64279,68254,60359,62021,60079,65474,60697,64232 ,67498,61955,63639,60315,67534,65650,61116,64733,68085,70608,64509,69621,69535,66355,61629,66979,61463,65842,59948,65401,60448,59742,68792,60559,60428 ,61340,65320,62916,67482,69445,61804,61551,59955,59946,61524,68111,64443,65553,63130,65616,61467,61632,64255,69506,60060,59903,67721,60160,68143,65990 ,68689,70596,60149,61940,63762,61883,60291,61659,63393,62085,60829,61067,59225,64099,60204,66329,60004,60852,60393,66295,67473,59839,60774,60065,69543 ,65261,60180,59495,64300,65661,69100,68690,63382,67519,62212,61065,68991,64096,67452,64299,65390,63323,62090,64777, 66431,60295,67641,64126,68832,60546,68321,60608,68174,63024,60470,64471,61183,64529,59807,59649,67741,68921,69513,60637,65440,62659,67932,62253,65469, 69687,63716,60323,61933,61662,65782,65962,60378,68387,59175,60689,70598,60126,63406,65879,59467,64503,67463,60316,62222,68973,62103,60868,64451,66279, 59470,59966,62787,66483,61110,59235,60207,70668,64690,62066,68896,65577,60842,63503,68700,61908,59607,64485,65473,60279,66210,66284,59693,63437,59993, 62426,62076,66890,67475,61810,62729,60820,61990,66278,59585,65481,65302,69668,63302,68855,64223,59694,64403,61815,70593,69021,63000,59641,66348,68045, 60553,64389,65800,60847,60253,62037,61720,60083,65441,67518,66395,67991,60145,68244,59756,65845,59638,68916,59910,67895,67889,68187,60832,63092,66878, 67459,60945,62656,60150,63480,60551,66479,60899,69043,60302,63667,68388,69128,65668,67634,62616,65212,61555,66265,64349,69551,67679,63071,60790,65335, 69444,60410,65789,60693,68002,67794,66191,65586,62867,60994,66390,66915,59870,62552,65246,68083,5929 4,61111,69675,61475,65865,61591,64088,65994,66489,65705,67616,61970,61071,66888,63725,65215,68975,62568,59490,60679,60897,66117,66054,60963,68093, 62503,61617,59291,60369,64390,59496,60805,66004,68168,68407,60039,59518,60732,68020,64551,59342,65795,66926,64323,64683,69548,63637,63021,65651,59575, 63449,62715,65365,67877,69108,70583,68986,60759,67468,62232,63810,60047,65720,65585,68389,68319,68744,65337,61939,65470,59488,67546,59622,65672,69039, 68332,63444,61083,60070,60552,65190,60346,63481,59686,60841,62149,64854,59962,66440,69676,70580,60128,65294,63332,64302,65763,67880,59873,66304,67774, 64143,59879,59168,62984,66472,60462,62508,66407,62655,64326,66371,67738]" remote_destination_count":1," local_destination_address_ids":[],&# 34; assigned_to":" ABCD"" local_destination_count":0," SOURCE_COUNT":435," START_TIME":1447320116826,& #34; ID":23159" destination_networks":["其他&#34],"不活动的":真,"保护&#34 ;:真正," policy_category_count":0,"说明":"防火墙许可证\ n"," category_count":1," domain_id&# 34;:8,"相关性":1," DEVICE_COUNT":1," security_category_count":1," flow_count":0,& #34; EVENT_COUNT":6109," offense_source":" 127.0.0.1""状态":" CLOSED",& #34;量值":0,"严重性":0," username_count":0," closing_user":" AMCD&#34 ;, "后续":假," closing_reason_id":104," CLOSE_TIME":1447759629000," source_network":"其他" ," last_updated_time":1447327073500,"类别":["防火墙许可证"]," crime_type":1}

我在尝试打印时遇到以下错误:

Traceback (most recent call last):
  File "E:\Offence automation\API_0.3.py", line 115, in <module>
    c = datetime.datetime.fromtimestamp(ct/1000.0).strftime('%Y-%m-%d %H:%M:%S')
TypeError: unsupported operand type(s) for /: 'NoneType' and 'float'

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。

这就是为什么因为在close_time中,有时值将为null。所以我们不能分割字符串。然后我像这样使用: `

ws['I1'] = 'Closed Time'
    column_cell = 'I'
    #ct = x['close_time']
    #print (ct)
    if x['close_time'] == None:
        print (0)
        ws[column_cell+str(row+2)] = 'None'
    elif x['close_time'] != None:
        ct = x['close_time']
        c = (datetime.datetime.fromtimestamp(ct/1000.0).strftime('%Y-%m-%d %H:%M:%S'))
        ws[column_cell+str(row+2)] = (c)+' GMT'
        print (ct)`

现在工作正常。

谢谢大家。