2017-04-17 55 views
-1

我有這種形式如何在Python中使用numpy或pandas來讀取和分割數據?

34950388,"Bodycology Scarlet Kiss Fragrance Mist, 8 fl oz","<p>Turn heads  
with our intoxicating fragrance made with rich pomegranate, luscious peach 
and warm vanilla. Be confident, and you're sure to be a showstopper! 
</p>","<B>Bodycology Scarlet Kiss Fragrance Mist, 8 fl oz:</b><ul><li>For 
a splash of fragrance and energy, spritz lightly on wrists, neck and 
shoulders.<li>Top Notes: Juicy Pomegranate, Peach Skin, Macintosh 
Apple<li>Middle Notes: Red Peony, Osmanthus, Marshmallow Creme<li>Dry 
Notes: Rich, Vanilla Rum, Oakwood, Scarlet Musk</ul>",None,, 

我想讀這個數據的數據,並把它分解基於文本的逗號,使得拆分後,我的結果應該是:

34950388 

"Bodycology Scarlet Kiss Fragrance Mist, 8 fl oz" 

"<p>Turn heads with our intoxicating fragrance made with rich  
pomegranate, luscious peach and warm vanilla. Be confident, and you're 
sure to be a showstopper!</p>" 

"<B>Bodycology Scarlet Kiss Fragrance Mist, 8 fl oz:</b><ul><li>For 
a splash of fragrance and energy, spritz lightly on wrists, neck and 
shoulders.<li>Top Notes: Juicy Pomegranate, Peach Skin, Macintosh 
Apple<li>Middle Notes: Red Peony, Osmanthus, Marshmallow Creme<li>Dry 
Notes: Rich, Vanilla Rum, Oakwood, Scarlet Musk</ul>" 

None 

我該怎麼辦這不使用Python的任何庫(Numpy,Pandas)?

+1

你嘗試了什麼,什麼不起作用? – Vallentin

+0

哪裏來自你的數據?你嘗試了什麼? – Dadep

回答

0

您可以通過不加引號逗號使用正則表達式拆分:

,(?=(?:[^"]*"[^"]*")*[^"]*$) 

假設你的數據是text,然後使用re模塊和re.split

import re 
for line in re.split(',(?=(?:[^"]*"[^"]*")*[^"]*$)', text): 
    print(line) 
    print() 

,輸出:

34950388 

"Bodycology Scarlet Kiss Fragrance Mist, 8 fl oz" 

"<p>Turn headswith our intoxicating fragrance made with rich pomegranate, luscious peach and warm vanilla. Be confident, and you're sure to be a showstopper! </p>" 

"<B>Bodycology Scarlet Kiss Fragrance Mist, 8 fl oz:</b><ul><li>For a splash of fragrance and energy, spritz lightly on wrists, neck and shoulders.<li>Top Notes: Juicy Pomegranate, Peach Skin, Macintosh Apple<li>Middle Notes: Red Peony, Osmanthus, Marshmallow Creme<li>Dry Notes: Rich, Vanilla Rum, Oakwood, Scarlet Musk</ul>" 

None 
相關問題