2017-07-31 65 views
-1

我有两个Python列表:合并Python列表和返回JSON

cols = ['InfoKey', 'InfoData'] 

vals = ['Row 1: [LANGUAGE SUPPORT MODE, Standard]', 'Row 2: [RELEASE, 15.00.04.01]', 'Row 3: [VERSION, 15.00.04.01]'] 

如何将它们合并,并返回类型的JSON:

{ 
"data": [{ 
     "Infokey": "LANGUAGE SUPPORT MODE", 
     "InfoData": "Standard" 
    }, 
    { 
     "Infokey": "RELEASE", 
     "InfoData": "15.00 .04 .01" 
    }, 
    { 
     "Infokey": "VERSION", 
     "InfoData": "15.00 .04 .01" 
    } 
] 

}

+2

你应该发布自己的代码尝试,并清楚地解释你卡在哪里。我承认这个问题有点棘手,因为像LANGUAGE SUPPORT MODE和Standard这样的内部字符串没有被引用。据推测,他们从不包含引号,逗号或方括号,否则很难正确解析数据。 –

回答

0

您可以使用regex这里提取[...]之间的字符串。这里的样本实施例

>>> import re 
>>> vals = ['Row 1: [LANGUAGE SUPPORT MODE, Standard]', 'Row 2: [RELEASE, 15.00.04.01]', 'Row 3: [VERSION, 15.00.04.01]'] 
>>> for s in vals: 
...  a, b = re.findall('\[([^"]*)\]', s)[0].split(', ') 
...  print('Infokey: ', a) 
...  print('InfoData: ', b) 
... 
Infokey: LANGUAGE SUPPORT MODE 
InfoData: Standard 
Infokey: RELEASE 
InfoData: 15.00.04.01 
Infokey: VERSION 
InfoData: 15.00.04.01 

PS:您需要存储我的控制台上打印到dict输出,并且每个字典对象的追加到列表中。我正准备离开它去做。

1

正如@PM所说,这在很大程度上取决于所描述的数据格式。

import json 

cols = ['InfoKey', 'InfoData'] 
vals = [ 
    'Row 1: [LANGUAGE SUPPORT MODE, Standard]', 
    'Row 2: [RELEASE, 15.00.04.01]', 
    'Row 3: [VERSION, 15.00.04.01]' 
] 

master = [] 

for item in vals: 
    data = item[item.find('[')+1:item.find(']')] 
    parts = data.split(',') 
    master.append({cols[0]: parts[0].strip(), 
     cols[1]: parts[1].strip()}) 

print json.dumps({'data': master}) 
1

这个什么:

import json 
l = [] 
for v in vals: 
    info = v.split(': ')[1].replace('[', '').replace(']', '') 
    key, data = info.split(', ') 
    d = {} 
    d["InfoKey"] = key 
    d["InfoData"] = data 
    l.append(d) 

json_dict = {"data": l} 

print json.dumps(json_dict) 

结果:

{"data": [{"InfoData": "Standard", "InfoKey": "LANGUAGE SUPPORT MODE"}, {"InfoData": "15.00.04.01", "InfoKey": "RELEASE"}, {"InfoData": "15.00.04.01", "InfoKey": "VERSION"}]} 
+0

fixed @ PM2Ring。 – JacobIRR

0

这也将工作

cols = ['InfoKey', 'InfoData'] 
vals = ['Row 1: [LANGUAGE SUPPORT MODE, Standard]', 'Row 2: [RELEASE, 15.00.04.01]', 'Row 3: [VERSION, 15.00.04.01]'] 
myData = [] 
for v in vals: 
    v = v.split(': ')[1].split(', ') 
    myData.append(dict(zip(cols, [v[0][1:], v[1][:-1]]))) 
json_data = json.dumps({'data':myData}) 
print(json_data) 

输出:

{"data": [{"InfoKey": "LANGUAGE SUPPORT MODE", "InfoData": "Standard"}, {"InfoKey": "RELEASE", "InfoData": "15.00.04.01"}, {"InfoKey": "VERSION", "InfoData": "15.00.04.01"}]} 
0

下面的代码假定在vals的数据是安全的,即,像LANGUAGE SUPPORT MODEStandard内字符串从未包含逗号或方括号中。

我们使用str.split方法与str.strip一起简单地解析字符串以摆脱不需要的空间。然后,我们可以通过使用cols中包含的密钥进行压缩变成字典。

import json 

cols = ['InfoKey', 'InfoData'] 

vals = [ 
    'Row 1: [LANGUAGE SUPPORT MODE, Standard]', 
    'Row 2: [RELEASE, 15.00.04.01]', 
    'Row 3: [VERSION, 15.00.04.01]' 
] 

data = [] 
for line in vals: 
    _, lst = line.split(':') 
    a = [u.strip() for u in lst.strip()[1:-1].split(',')] 
    data.append(dict(zip(cols, a))) 

json_obj = {'data': data} 
print(json.dumps(json_obj, indent=4)) 

输出

{ 
    "data": [ 
     { 
      "InfoKey": "LANGUAGE SUPPORT MODE", 
      "InfoData": "Standard" 
     }, 
     { 
      "InfoKey": "RELEASE", 
      "InfoData": "15.00.04.01" 
     }, 
     { 
      "InfoKey": "VERSION", 
      "InfoData": "15.00.04.01" 
     } 
    ] 
} 

当然,我们可以使用列表解析创建data,但我想你会同意,以前的版本是更多可读。 :)

data = [dict(zip(cols, (u.strip() for u in line.split(':')[1].strip()[1:-1].split(',')))) for line in vals]