我有一个文件,看起来像这样:就是这个样子。
Pokemon HP Attack_Points Type Weakness
Bulbasaur 45 49 Grass Fire
Ivysaur 60 62 Grass Fire
Venusaur 80 82 Grass Fire
我正在尝试从随机行生成列表,以下是我的代码:
from random import randint
open_pokedex=open("pokedex.txt","r")
p1_p1=list()
pokemon_selection=(randint(1,40))
p1_p1.append(open_pokedex.readline()[pokemon_selection])
但是这并没有起作用,有人有什么想法吗?
输出:这次它生成了31并且显示为this = ['\t'] p1_p1。问题已解决
但由于txt文件不好,它会产生['Charmander \ t39 \ t52 \ Fire \ Water \ n'],如何在不干扰文件本身的情况下删除这些制表符
readlines()
而不是readline()
。你应该展示一下你的代码当前的输出,这样我们就有更多信息了。说“它不起作用”并没有什么用,因为我们需要知道它以何种方式不起作用。 - Tom Dalton