如何在Python中读取和分割数据而不使用numpy或pandas?

2024-04-20 03:18:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这张表格的数据

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),我如何做到这一点?你知道吗


Tags: and数据withlikissulturnnotes
1条回答
网友
1楼 · 发布于 2024-04-20 03:18:10

可以使用regex按逗号拆分,而不是用引号括起来:

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

假设您的数据是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

相关问题 更多 >