如何将数据从文件导入类对象?

2024-09-19 23:43:26 发布

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

我是python新手,学习类和对象

我有一个文件,其中包含大量csv格式的口袋妖怪数据-示例如下:

Number,Name,Type1,Type2,HP,Attack,Defense,SpecialAtk,SpecialDef,Speed,Generation,Legendary,Mega
1,Bulbasaur,Grass,Poison,45,49,49,65,65,45,1,FALSE,FALSE
2,Ivysaur,Grass,Poison,60,62,63,80,80,60,1,FALSE,FALSE
3,Venusaur,Grass,Poison,80,82,83,100,100,80,1,FALSE,FALSE
6,Mega Charizard Y,Fire,Flying,78,104,78,159,115,100,1,FALSE,TRUE
10,Caterpie,Bug,,45,30,35,20,20,45,1,FALSE,FALSE
11,Metapod,Bug,,50,20,55,25,25,30,1,FALSE,FALSE
12,Butterfree,Bug,Flying,60,45,50,90,80,70,1,FALSE,FALSE
13,Weedle,Bug,Poison,40,35,30,20,20,50,1,FALSE,FALSE
14,Kakuna,Bug,Poison,45,25,50,25,25,35,1,FALSE,FALSE
20,Raticate,Normal,,55,81,60,50,70,97,1,FALSE,FALSE

我已经定义了我的口袋妖怪类并打开了以下文件:

pokedex = open('../resource/lib/public/pokedex.csv', 'r')
for line in pokedex:
    row = line.strip().split(",")

class Pokemon:
    def __init__(self, Number, Name, Type1, Type2 = "" , HP, Attack, Defense,
                SpecialAtk, SpecialDef, Speed,Generation, Legendary, Mega):

        self.Number = Number
        self.Name = Name
        self.Type1 = Type1
        self.Type2 = Type2
        self.HP = HP
        self.Attack = Attack
        self.Defense = Defense
        self.SpecialAtk = SpecialAtk
        self.SpecialDef = SpecialDef
        self.Speed = Speed
        self.Generation = Generation
        self.Legendary = Legendary
        self.Mega = Mega

    def total_stats(self):
        total = self.HP+self.Attack+self.Defense+self.SpecialAtk+self.SpecialDef+self.Speed
        return total

我想用这些数据回答一系列问题,例如:

- What Pokemon has the highest HP statistic?
- Excluding Pokemon that are either Mega or Legendary, what Pokemon has the highest Defense statistic?
- Among Legendary Pokemon, what is the most common type? Include both Type1 and Type2 in your count.
-In terms of the sum of all six stats (HP, Attack, Defense, Special Attack, Special Defense, and Speed), what is the weakest Legendary Pokemon? If there is a tie, list any of the tying Pokemon.

我该怎么做呢?我不知道如何将文件中的数据链接到口袋妖怪类。请帮忙


Tags: theselffalsehpspeedpokemondefenseattack
2条回答

您可以将数据存储在列表中This isn't something new。那么,你会这样做吗

class Pokemon:
    # Read more about the adjustment made by removing the default value in Type2 - https://stackoverflow.com/a/48370634/5675325
    def __init__(self, Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega):
        self.Number = Number
        self.Name = Name
        self.Type1 = Type1
        self.Type2 = Type2
        self.HP = HP
        self.Attack = Attack
        self.Defense = Defense
        self.SpecialAtk = SpecialAtk
        self.SpecialDef = SpecialDef
        self.Speed = Speed
        self.Generation = Generation
        self.Legendary = Legendary
        self.Mega = Mega

pokemon_list = []

with open('pokemon.csv', newline='') as csv_file:
    #reader = csv.reader(csv_file)
    #next(reader, None)
    results = []
    for line in csv_file:
        words = line.split(',')
        results.append((words[0:]))
    print(results)
    for Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega in results:
        pokemon_list.append(Pokemon(Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega))

pokemon_list.pop(0) # To remove the object created using headers

print(pokemon_list)

# Then do something with the pokemon_list.

在控制台中,我们将看到如下内容

Console after execution

如果我们想看看口袋妖怪列表变量是什么

Pokemon list

单击以查找索引为3的列表项中的实例

Pokemon

正如您所提到的,要想让口袋妖怪拥有最高的HP,您需要使用operator.attrgetter()来获得该值

from operator import attrgetter

max_HP = max(pokemon_list, key=attrgetter('HP')).HP
max_ind = [obj for obj in pokemon_list if obj.HP == max_HP]

如果我们调整代码以包含此部分

from operator import attrgetter

class Pokemon:
    # Read more about the adjustment made by removing the default value in Type2 - https://stackoverflow.com/a/48370634/5675325
    def __init__(self, Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega):
        self.Number = Number
        self.Name = Name
        self.Type1 = Type1
        self.Type2 = Type2
        self.HP = HP
        self.Attack = Attack
        self.Defense = Defense
        self.SpecialAtk = SpecialAtk
        self.SpecialDef = SpecialDef
        self.Speed = Speed
        self.Generation = Generation
        self.Legendary = Legendary
        self.Mega = Mega

pokemon_list = []

with open('pokemon.csv', newline='') as csv_file:
    #reader = csv.reader(csv_file)
    #next(reader, None)
    results = []
    for line in csv_file:
        words = line.split(',')
        results.append((words[0:]))
    #print(results)
    for Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega in results:
        pokemon_list.append(Pokemon(Number, Name, Type1, Type2, HP, Attack, Defense, SpecialAtk, SpecialDef, Speed, Generation, Legendary, Mega))

pokemon_list.pop(0)

#print(pokemon_list)

max_HP = max(pokemon_list, key=attrgetter('HP')).HP
max_ind = [obj for obj in pokemon_list if obj.HP == max_HP]

print(max_ind)

我们将在控制台中获得以下内容

Console after code adjustment

所以我们可以看到列表max_ind只有一个值

Highest HP Pokemon

最大生命是80,拥有它的口袋妖怪是维努索

要排除超级或传奇的口袋妖怪,请从here获得灵感,并将其与前面提到的内容结合起来

您可以通过创建一个实例来完成

bulbasaur = Pokemon(1,'Bulbasaur','Grass','Poison',45,49,49,65,65,45,1,FALSE,FALSE 2)

相关问题 更多 >