Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
389 views
in Technique[技术] by (71.8m points)

Python creating list of lists from excel using openpyxl

I am trying to input a file from excel and store it in a list for a card collection program.I can place everything into one big list but im not sure how to parse out my list and group the moves together.

I have a class called Card which adds cards to the deck list (this works) however i want read an excel file which has a collection of cards and eventually add and remove cards from the excel sheet in python.

import openpyxl    
Excel example:
Name   Type   HP  Move 1 Move 2 Move3 Move4 Move5 Shiny status
<---random data in cells--->

class Deck:

def __init__(self):
    self.deck=[]
    self.moves=[]
  
def inputFromFile(self, fileName):
"""Read the file and store card info in self.deck"""
    print("please input the file")
    self.fileName=fileName
    book=openpyxl.load_workbook(self.fileName)
    sheet=book.active
    #start from min row 2 so i dont include the cell titles e.g name,card type ect..
    for row in sheet.iter_rows(min_row=2,values_only=True):
        self.deck.append(row)
        #trying to group the moves together
        #for cell in sheet.iter_rows(min_row=2,min_col=5,values_only=True):
            #self.moves.append([cell[0],cell[1]])
    #print(self.moves) TESTING
    print(self.deck)


def main():
#       name ,  type, hp, move lists between 1-5 moves, shiny true/false
c1=Card("DAVE","mage",500,[["Fireball",50],["Flame",80],["Waterblast",25]],True)
D1=Deck()

#I want to import my deck and i want to to be in the format like the c1
D1.inputFromFile("sample.xlsx")

#this works fine on its own
    D1.addCard(c1)

if __name__ == "__main__":
    main()

Basically i want to place the rows from the excel document in self.deck[] and for each row group the moves so that they are in a list of list e.g [["Fireball",50],["Flame",80],["Waterblast",25]].

question from:https://stackoverflow.com/questions/65644199/python-creating-list-of-lists-from-excel-using-openpyxl

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...