Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/331.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何将嵌套列表中的数据提取到CSV或表格中?_Python_Csv - Fatal编程技术网

Python 如何将嵌套列表中的数据提取到CSV或表格中?

Python 如何将嵌套列表中的数据提取到CSV或表格中?,python,csv,Python,Csv,我目前正在制作一个Pokémon数据库应用程序,为了防止手动输入大约50000个Pokémon移动链接,我希望将此过程自动化。 我在网上找到了一个免费提供的数据集,其中存在神奇宝贝移动链接,但采用嵌套列表格式 我在这里复制并粘贴了部分数据集: 最后,我希望有一个表(最好以CSV/Excel可读格式存储),如下所示: | pokemonname | move | movelearnmethod | |-------------|---------|-----------------| | b

我目前正在制作一个Pokémon数据库应用程序,为了防止手动输入大约50000个Pokémon移动链接,我希望将此过程自动化。 我在网上找到了一个免费提供的数据集,其中存在神奇宝贝移动链接,但采用嵌套列表格式

我在这里复制并粘贴了部分数据集:

最后,我希望有一个表(最好以CSV/Excel可读格式存储),如下所示:

| pokemonname | move    | movelearnmethod |
|-------------|---------|-----------------|
| bulbasaur   | amnesia | 6E              |
| bulbasaur   | attract | 6M              |
| bulbasaur   | bind    | 6T              |
| bulbasaur   | endure  | 6E              |
| bulbasaur   | endure  | 6T              |
我曾尝试在Python中使用split()命令开始按分隔符进行拆分,但有多个不同的分隔符,我不知道如何解决这个问题。 任何帮助都将不胜感激!谢谢

更新:


我只是想澄清一下,如果神奇宝贝在一次移动中有多个movelearnmethod,比如bulbasaur的Persiste,它的movelearnmethod既有“6E”又有“6T”,那么它会为第二个movelearnmethod创建一个单独的行,如上表所示。

我不明白“多个分隔符”是什么意思。逗号在很多地方都有使用,但冒号或右括号可能是很好的分隔符

另一种方法是使用正则表达式,因此,使用perl而不是python

友好的,
亚历克西斯

示例数据非常类似于Python字典,但没有引用键。您可以使用一些正则表达式来解决这个问题,然后将其作为Python字典引用,解析非常简单

import re
import ast
data = """{bulbasaur:{learnset:{amnesia:["6E"],attract:["6M"],bind:["6T"],block:[],bodyslam:[],bulletseed:[],captivate:[],charm:["6E"],confide:["6M"],curse:["6E"],cut:["6M"],defensecurl:[],doubleedge:["6L027"],doubleteam:["6M"],echoedvoice:["6M"],endure:["6E","6T"],energyball:["6M"],facade:["6M"],falseswipe:[],flash:["6M"],frenzyplant:[],frustration:["6M"],furycutter:[],gigadrain:["6E","6T"],grassknot:["6M"],grasspledge:["6T"],grasswhistle:["6E"],grassyterrain:["6E"],growl:["6L003"],growth:["6L025"],headbutt:[],hiddenpower:["6M"],ingrain:["6E"],knockoff:["6T"],leafstorm:["6E"],leechseed:["6L007"],lightscreen:["6M"],magicalleaf:["6E"],mimic:[],mudslap:[],naturalgift:[],naturepower:["6E","6M"],petaldance:["6E"],poisonpowder:["6L013"],powerwhip:["6E"],protect:["6M"],razorleaf:["6L019"],rest:["6M"],"return":["6M"],rocksmash:["6M"],round:["6M"],safeguard:["6M"],secretpower:["6M"],seedbomb:["6L037","6T"],skullbash:["6E"],sleeppowder:["6L013"],sleeptalk:["6M"],sludge:["6E"],sludgebomb:["6M"],snore:["6T"],solarbeam:["6M"],strength:["6M"],stringshot:[],substitute:["6M"],sunnyday:["6M"],swagger:["6M"],sweetscent:["6L021"],swordsdance:["6M"],synthesis:["6L033","6T"],tackle:["6L001a"],takedown:["6L015"],toxic:["6M"],venoshock:["6M"],vinewhip:["6L009"],weatherball:[],worryseed:["6L031","6T"]}}}"""
dict_data = re.sub('(\w+):', '"\\1":', data)
move_data = ast.literal_eval(dict_data)
for pokemonname in move_data.keys():
    learn_set = move_data[pokemonname]['learnset']
    for move in learn_set.keys():
        for method in learn_set[move]:
            print 'pokemonname: {0}, move: {1}, movelearnmethod: {2}'.format(pokemonname, move, method)


pokemonname: bulbasaur, move: sludgebomb, movelearnmethod: 6M
pokemonname: bulbasaur, move: venoshock, movelearnmethod: 6M
pokemonname: bulbasaur, move: doubleteam, movelearnmethod: 6M
pokemonname: bulbasaur, move: confide, movelearnmethod: 6M
pokemonname: bulbasaur, move: rest, movelearnmethod: 6M
pokemonname: bulbasaur, move: sludge, movelearnmethod: 6E
pokemonname: bulbasaur, move: growth, movelearnmethod: 6L025
pokemonname: bulbasaur, move: grassknot, movelearnmethod: 6M
pokemonname: bulbasaur, move: facade, movelearnmethod: 6M
pokemonname: bulbasaur, move: return, movelearnmethod: 6M
pokemonname: bulbasaur, move: attract, movelearnmethod: 6M
pokemonname: bulbasaur, move: echoedvoice, movelearnmethod: 6M
pokemonname: bulbasaur, move: substitute, movelearnmethod: 6M
pokemonname: bulbasaur, move: growl, movelearnmethod: 6L003
pokemonname: bulbasaur, move: curse, movelearnmethod: 6E
pokemonname: bulbasaur, move: powerwhip, movelearnmethod: 6E
pokemonname: bulbasaur, move: ingrain, movelearnmethod: 6E
pokemonname: bulbasaur, move: gigadrain, movelearnmethod: 6E
pokemonname: bulbasaur, move: gigadrain, movelearnmethod: 6T
pokemonname: bulbasaur, move: worryseed, movelearnmethod: 6L031
pokemonname: bulbasaur, move: worryseed, movelearnmethod: 6T
pokemonname: bulbasaur, move: flash, movelearnmethod: 6M
pokemonname: bulbasaur, move: takedown, movelearnmethod: 6L015
...

一旦您有了这些数据,我建议您看看Python的CSV编写器:。创建writer对象后,您可以通过调用writerow来替换上面的打印。

这不是答案Alexist这太棒了!非常感谢。我只有一个小问题——使用多种学习方法的动作似乎只能挽救第一个。例如,口袋妖怪名称:bulbasaur,move:worryseed,movelearnmethod:6L031Ah是的,我本来就错过了。编辑处理多种学习方法。谢谢!你救了我和我的手好几百个小时。非常感谢你!