Python JSON解析问题

时间:2015-05-27 07:45:41

标签: python json parsing

我正在尝试解析JSON文件,但我无法准确地得出我期望的结果!

import json
import datetime
import sys
json_data=open('1.json')
server_details = json.load(json_data)
json_data.close()
for App_ID in server_details:
    type=[]
    x=0
    if "content-security-policy" in server_details[App_ID]:
        type.append("content-security-policy")
    if "x-frame-options" in server_details[App_ID]:
        type.append("x-frame-options")
    if "strict-transport-security" in server_details[App_ID]:
        type.append("strict-transport-security")
    if "x-content-type-options" in server_details[App_ID]:
        type.append("x-content-type-options")
    if "x-xss-protection" in server_details[App_ID]:
        type.append("x-xss-protection")
    headers_join=",".join(str(x) for x in type)
    header_complete=App_ID+","+headers_join
    for header in header_complete:
        print header,

当我尝试打印时,我得到结果:

1 1 4 7 , 2 1 0 , 2 4 5 8 , s t r i c t - t r a n s p o r t - s e c u r i t y 1 7 7 1 , 1 8 3 4 , 2 4 8 7 , 3 , 1 1 5 9 , 1 3 1 7 , 1 1 , 1 0 9 6 , 1 3 1 3 , 1 3 1 2 , 2 6 5 , 4 2 2 , 1 5 5 6 , 2 4 9 7 , 2 0 6 8 , 2 0 6 1 , 2 2 5 2 , 2 2 5 0 , 1 0 8 3 , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 9 8 , 7 0 4 , 3 1 0 , x - f r a m e - o p t i o n s , x - c o n t e n t - t y p e - o p t i o n s 8 6 , 7 9 2 , 2 3 0 3 , 1 2 5 4 , 2 1 7 1 , 2 3 8 5 , x - f r a m e - o p t i o n s 2 3 8 4 , 9 1 5 , 2 0 7 2 , x - f r a m e - o p t i o n s 1 4 0 1 , 1 4 0 0 , 3 0 0 , 1 3 3 4 , 1 1 3 5 , 7 8 6 , 2 0 4 1 , 1 8 4 3 , 1 6 4 9 , 1 7 2 3 , 1 8 4 5 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 0 0 1 , 2 2 9 9 , 2 5 9 1 , 4 5 6 , 1 3 4 7 , 1 1 6 0 , 1 7 0 , s t r i c t - t r a n s p o r t - s e c u r i t y 2 5 6 9 , 9 7 6 , 7 3 0 , c o n t e n t - s e c u r i t y - p o l i c y , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 4 2 8 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y 1 5 0 2 , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s 2 1 1 9 , 1 5 0 0 , 1 8 7 , x - f r a m e - o p t i o n s 7 3 6 , s t r i c t - t r a n s p o r t - s e c u r i t y 5 0 2 , 4 6 9 , 2 5 8 8 , 3 5 5 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 6 8 , 1 9 1 7 , 2 2 1 3 , 4 7 0 , 2 2 1 1 , 1 0 2 6 , 1 7 , 2 2 3 6 , 1 0 2 9 , x - f r a m e - o p t i o n s , x - c o n t e n t - t y p e - o p t i o n s , x - x s s - p r o t e c t i o n 1 1 6 5 , 8 8 7 , 2 6 3 8 , 2 4 3 3 , 5 5 6 , 2 6 3 2 , 2 6 2 6 , 2 0 8 5 , 1 6 1 7 , 2 3 4 8 , 1 4 4 7 , 1 1 1 5 , 2 5 0 8 , 1 2 9 2 , 3 2 8 , 6 1 0 , 7 2 , 1 0 4 1 , 1 9 7 7 , x - f r a m e - o p t i o n s , s t r i c t - t r a n s p o r t - s e c u r i t y 2 1 2 8 , x - f r a m e - o p t i o n s 1 6 0 6 , 1 6 8 1 , x - f r a m e - o p t i o n s 1 3 6 4 , 3 5 3 ,

确切结果必须如下:

1147,
210,
2458,strict-transport-security
1771,
1834,
2487,
3,
1159,
1317,
11,
1096,
1313,
1312,
265,
422,
1556,
2497,
2068,
2061,
2252,
2250,
1083,strict-transport-security,x-content-type-options,x-xss-protection
198,
704,
310,x-frame-options,x-content-type-options
86,
792,
2303,
1254,
2171,
2385,x-frame-options
2384,
915,
2072,x-frame-options
1401,
1400,
300,
1334,
1135,
786,
2041,
1843,
1649,

1 个答案:

答案 0 :(得分:4)

header_complete是一个字符串:

headers_join=",".join(str(x) for x in type)
header_complete=App_ID+","+headers_join

",".join()表达式生成一个字符串,然后您将应用程序ID预先挂起。

然后循环遍历该字符串以生成单独打印的每个字符,并使用逗号告诉print省略换行符:

for header in header_complete:
    print header,

删除循环;只需使用:

print header_complete

您实际上只是从per-AppId列表中过滤掉某些选项。您可以使用列表解析来代替一系列if语句:

for appid, headers in server_details.iteritems():
    htypes = [h for h in headers if h in (
                  'content-security-policy', 'x-frame-options',
                  'strict-transport-security', 'x-content-type-options',
                  'x-xss-protection')]
    print '{},{}'.format(App_ID, ','.join(htypes))