Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

genereting list from file python

I have a file that looks like this:its exactly like this

Pokemon     HP  Attack_Points   Type    Weakness
Bulbasaur   45     49     Grass Fire
Ivysaur     60     62     Grass Fire
Venusaur    80     82     Grass Fire

and I am trying to generate list from a random line and this is my code:

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])

but this is not working anyone have any ideas? output: it generetes 31 this time and shows p1_p1 as this = ['\t']

problem solved

but bcs of bad txt file it does this ['Charmander \t39\t52\tFire\tWater\n'] how to erase these tabs without messing with file itself

like image 536
Ciocomo Trombetta Avatar asked Jan 26 '26 01:01

Ciocomo Trombetta


1 Answers

Use linecache.getline. It does exactly what you want; getting line lineno from a file.

import linecache
from random import randint

pokemon_selection = randint(1, 40)
pokemon = linecache.getline("pokedex.txt", pokemon_selection)
like image 57
falsetru Avatar answered Jan 27 '26 14:01

falsetru



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!