2017-07-31 101 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]