pythonで線形リストを作りたいです。与えられたデータを一行ずつ読み取り、次の行のデータが前の行のデータの上に来るようにリストを作りたいです。つまり、データが逆順に並ぶようにしたいです。次のようなコードを書きましたが、エラーが出てしまいます。解決方法を教えていただきたいです。よろしくお願いいたします。
python
1class Player: 2 def __init__(self,number,name,profile,next): 3 self.number = number 4 self.name = name 5 self.profile = profile 6 self.next = next 7 8def print_info(p:Player): 9 while (p.next != None): 10 print("{:>2d}".format(int(p.number)),"{:str}".format(p.name),"{:str}".format(p.profile),end="") 11 p = p.next 12 print("") 13 14def insert_before(p:Player,bp:Player):#bpの人の前にpの人の名前を挿入 15 tmp = Player(p.number,p.name,p.profile,0) 16 p.number,p.name,p.profile = bp.number, bp.name, bp.profile 17 bp.number = tmp.number 18 bp.name = tmp.name 19 bp.profile = tmp.profile 20 bp.next = p 21 22data = [] 23 24f = open('zac_japan.txt','r',encoding = 'utf-8') 25data = f.readlines() 26f.close 27 28p = Player(0,0,0,0) 29d_old = p 30for n in data: 31 new = (n.split()) 32 d = Player(int(new[0]),new[1],new[2],0) 33 insert_before(d,d_old) 34 d_old = d 35print_info(d_old)
表示されているエラーコード
``ValueError: Invalid format specifier`
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/06/27 21:34