python - Value Error : invalid literal for int() with base 10: '' -
i'm new in python , don't know why i'm getting error sometimes.
this code:
import random sorteio = [] urna = open("urna.txt") y = 1 while y <= 50: sort = int(random.random() * 392) print sort while sort > 0: x = urna.readline() sort = sort - 1 print x sorteio = sorteio + [int(x)] y = y + 1 print sorteio
where urna.txt file on format:
1156 459 277 166 638 885 482 879 33 559
i'll grateful if knows why error appears , how fix it.
upon attempting read past end of file, you're getting empty string ''
cannot converted int.
>>> int('') traceback (most recent call last): file "<stdin>", line 1, in <module> valueerror: invalid literal int() base 10: ''
to satisfy requirement of selecting 50 random lines text value, if understand problem correctly:
import random open("urna.txt") urna: sorteio = [int(line) line in urna] # lines of file ints selection = random.sample(sorteio, 50) print selection
Comments
Post a Comment