首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Python将数组对象(字符串)转换为可工作列表

如何使用Python将数组对象(字符串)转换为可工作列表
EN

Stack Overflow用户
提问于 2021-09-02 16:08:29
回答 1查看 27关注 0票数 0

我已经解析了一些数据,当您执行df['text'].values时,示例单元格如下所示

代码语言:javascript
复制
['[\'Color is not claimed as a feature of the mark.\', \'"BRICKS"\', \'The mark consist of the stylized word "PIX" with an image of a toy block to the right and the stylized word "BRIX" appearing below.\', "Children\'s toys, namely, toy building and construction blocks and interlocking toy building and construction blocks", \'PIX BRICKS\']'],
      dtype=object)

然而,我想将数据分成多个列表,类似于这种格式,但每个列表包含的信息都在一个单元格中‘颜色’不是一个特征,‘砖块’...

代码语言:javascript
复制
20523203       Color is not claimed as a feature of the mark.
20523204                                             "BRICKS"
20523205    The mark consist of the stylized word "PIX" wi...
20523206    Children's toys, namely, toy building and cons...
20523207                                           PIX BRICKS

对如何做到这一点有什么建议吗?

EN

回答 1

Stack Overflow用户

发布于 2021-09-02 16:23:27

代码语言:javascript
复制
In [6]: data
Out[6]: ['[\'Color is not claimed as a feature of the mark.\', \'"BRICKS"\', \'The mark consist of the stylized word "PIX" with an image of a toy block to the right and the stylized word "BRIX" appearing below.\', "Children\'s toys, namely, toy building and construction blocks and interlocking toy building and construction blocks", \'PIX BRICKS\']']

In [7]: # you need to remove [ and ] from the data

In [8]: clean_data = data[0].strip('[').strip(']')

In [9]: clean_data
Out[9]: '\'Color is not claimed as a feature of the mark.\', \'"BRICKS"\', \'The mark consist of the stylized word "PIX" with an image of a toy block to the right and the stylized word "BRIX" appearing below.\', "Children\'s toys, namely, toy building and construction blocks and interlocking toy building and construction blocks", \'PIX BRICKS\''

In [10]: # then split the data

In [11]: clean_data.split(',')
Out[11]: 
["'Color is not claimed as a feature of the mark.'",
 ' \'"BRICKS"\'',
 ' \'The mark consist of the stylized word "PIX" with an image of a toy block to the right and the stylized word "BRIX" appearing below.\'',
 ' "Children\'s toys',
 ' namely',
 ' toy building and construction blocks and interlocking toy building and construction blocks"',
 " 'PIX BRICKS'"]
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/69033753

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档