2017-08-30 63 views
0

即時通訊使用scrapy得到這個JSON提取,但是desc有innt的amunt和數量類型,這可能是g,gr,kg,L等。我不知道它的可能提取這些數據並將其添加到其他字段中。JSON字符串提取到字段

一旦他創建了文件,如何在scrapy或單獨的進程中實現這一點。

P.S.我對JSON和scrapy完全陌生,我正在學習。

當前

{ 
'p_desc': ['Coffee 225 g '], 
'p_price': ['8.00'] 
} 

期望

{ 
'p_desc': ['Coffee'], 
'p_amount': [225] 
'p_amount_type': ['g'] 
'p_price': ['8.00'] 
} 

回答

0

事情是這樣的工作,如果該數據有規則的結構(即每遞減含有數量和金額類型爲最後兩個字段) 。如果不是,你可能不得不使用正則表達式。

一個觀察:如果每個值是唯一的,你並不需要一個列表,比如,你可以只使用'Coffee'代替['Coffee']

jsonData = { 
 
    'p_desc': ['Grain Black Coffee 225 g'], 
 
    'p_price': ['8.00'] 
 
} 
 

 
var p_desc, p_amount, p_amount_type; 
 

 
[p_amount_type, p_amount,...p_desc] = jsonData['p_desc'][0].split(" ").reverse(); 
 
jsonData["p_amount"] = [p_amount]; 
 
jsonData["p_amount_type"] = [p_amount_type]; 
 
jsonData["p_desc"] = p_desc.join(' '); 
 
console.log(jsonData);

此外,您可能需要刪除從描述中拖曳白色空間。

+0

感謝您的快速響應,字段p_desc並不總是3個值,但las 2始終是金額和金額類型。 黑咖啡100克 穀物黑咖啡1公斤 甜白糖棉花糖1毫升 等 – Bestialus

+0

這是我正在尋找。它解決了這個問題。這可以如何應用於文檔。 – Bestialus