list - how to write random numbers to file in python -
i store 100 randomly drawn numbers in file. how can write random numbers list, using file? how can read file? how can separate draw numbers
import random draw = [] while true: numbers_lotto = random.randint(1,50) draw.append(numbers_lotto) if len(draw) == 5 # numbers? break
import random file_name = 'random_numbers.txt' open ('file_name', 'w') a_file: in range (100): a_file.write ('{}\n'.format (random.random ())) open ('file_name', 'r') a_file: a_list = [float (word) word in a_file.read () .split ()] print (a_list)
or
import random import pickle file_name = 'random_numbers.txt' open (file_name, 'wb') a_file: pickle.dump ([random.random () in range (100)], a_file) open (file_name, 'rb') a_file: a_list = pickle.load (a_file) print (a_list)
Comments
Post a Comment