【发布时间】:2015-12-19 19:54:09
【问题描述】:
我是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,663 ,60455,69055546554555455455455455555555855455555455855555558585555855858558558558585855858585865865858658586585865858658658565858586585858586585858586585858585858658658586586586686586585865866586586865865866586865865866865865886586588658686588658965 ,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,6085,6085,6085,60855085,6085,6085,6060856608566085660856608566085 ,69100,68690,63382,67519,62212,61065,68991,64096,67452,64299,65390,63323,62090,64777,66431,60295,6764 1,64126,68832,60546,68321,60608,68174,63024,60470,64471,61183,64529,59807,59649,67741,68921,69513,60637,65440,62659,657932,96759,657932,9676 603259805988 ,,619805980598980598859888598885885985888588598858885885885858859858588588588585885858859859858859858858585885858585885985859858588598585985985985859859858859885985885985985858588598585858588585985859858585858858585 6278085 ,,6198085 ,,6198085 ,,5.680855980855858558855585585855855855858585585548585585585855855585655855858585585855585855855855465 668906080 ,,615808085 ,,6180658588585885858585858585858585585858555855858558585585858558585855858558558585858585858585858585855585858558585855 65808019801980,6080858588585885858585885858585858585858585858585858588585858585858585858585858585858585858585858585858585858585858585858585858585858585858585858585858585 6265665,6019808989808980898050498558558558555855555855555555555555555555555555555555555855 65789,60693,68002,67794,66191,65586,62867,60994,66390,66915,59870,62552,65246,68083,59294,61111,69675,61 475 ,,611650659805988898988980598888898888898888988988888598985858898598889888598598885898598589858598598598585985 6030805 ,,68050505985 ,,660805985 ,,6608059859859859859859858585858585858558585858858585585855858588585585855858588585585858585885855858585885858585858585858585585858558585858558585858585858558855 678080685 ,,619805585898085 ,,61988585885885885885888585885885858858585858588558588858585858585858588585885858885885885885885885858588588585885885885858585885885858588585858858585885858858585 60070505050505050505059808059805059858585805058585858059858858588585858858585858588585885858588585858585858585985885858858585985858585858585858585858585858585858585858585858585858585858585858585858585858585858585858585 62984,66472,60462,62508,66407,62655,64326,66371,67738],"remote_destination_count":1,"local_destination_address_ids":[],"assigned_to":"abcd","local_destination_count":0,"source_count": 435,"start_time":1447320116826,"id":23159,"destination_networks":["other"],"inactive":true,"protected":true,"policy_category_count":0,"description":"防火墙许可\ n","category_count":1,"domain_id":8,"relevance":1,"dev ice_count":1,"security_category_count":1,"flow_count":0,"event_count":6109,"offense_source":"127.0.0.1","status":"CLOSED","magnitude":0,"severity" :0,"username_count":0,"closing_user":"amcd","follow_up":false,"closing_reason_id":104,"close_time":1447759629000,"source_network":"other","last_updated_time":1447327073500," categories":["Firewall Permit"],"offense_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'
【问题讨论】:
-
错误信息很清楚。
ct是None,您尝试将其除以1000.0。 -
@timgeb 不分割 ct。我无法将纪元时间更改为 GMT 时间。这就是我这样使用的原因。如果时间是“1450195200000”,我想要“1450195200”。然后只有我打印确切的值。
-
你搞错了。如果没有
ct是一个数字,你就不能分割它。 -
我无法理解。但我使用相同的方法将
"start_time":1447320116826值转换为 GMT,它工作正常。我很困惑我在哪里做错了。两者都是纪元时间转换为格林威治标准时间。 @timgeb -
@timgeb 任何其他想法。如何解决这个问题。?