清理嵌套列表
我有一个巨大的混乱的嵌套列表,看起来像这样,只是更长:
fruit_mess = [['watermelon,0,1.0\n'], ['apple,0,1.0\n'], ['"pineapple",0,1.0\n'], ['"strawberry, banana",0,1.0\n'], ['peach plum pear,0,1.0\n'], ['"orange, grape",0,1.0\n']]
最终我想要看起来像这样的东西:
neat_fruit = [['watermelon',0,1.0], ['apple',0,1.0], ['pineapple',0,1.0], ['strawberry, banana',0,1.0], ['peach plum pear',0,1.0], ['orange, grape',0,1.0]]
但我不知道如何处理引号中的双引号以及如何将水果从数字,尤其是用逗号分隔一些水果。我尝试了很多事情,但一切似乎都让事情变得更加混乱。任何建议将不胜感激。
I have a huge mess of a nested list that looks something like this, just longer:
fruit_mess = [['watermelon,0,1.0\n'], ['apple,0,1.0\n'], ['"pineapple",0,1.0\n'], ['"strawberry, banana",0,1.0\n'], ['peach plum pear,0,1.0\n'], ['"orange, grape",0,1.0\n']]
Ultimately I want something that looks like this:
neat_fruit = [['watermelon',0,1.0], ['apple',0,1.0], ['pineapple',0,1.0], ['strawberry, banana',0,1.0], ['peach plum pear',0,1.0], ['orange, grape',0,1.0]]
but I'm not sure how to deal with the double quotes in the quotes and how to split the fruits from the numbers, especially with the commas separating some of the fruits. I've tried a bunch of things, but everything just seems to make it even more of a mess. Any suggestions would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
使用 csv 模块(在标准库中)来处理名称中带有逗号的双引号水果:
Use the
csv
module (in the standard library) to handle the double-quoted fruits with commas in their names:另一种简单的解决方案:
One more simple solution:
基于正则表达式的解决方案:
A regex-based solution: