I'm trying to create a random list of integers; but this list should be held constant once generated, rather than being generated anew every time I run the program. To do so, my idea is to generate it once, write it to a file, comment out that part, then read it back everytime I use it.
Unfortunately, I can't get reading & writing integers to work. Here's the simplest way to exhibit the problem.
I first write a list of integers as follows
learning=[]
for i in range(20):
learning.append(i)
np.savetxt('learning.txt',learning)
This does create the txt file and populate it with numbers; but they appear as 0.00000000000e+00
, 1.000000000000e+00
, etc. Writing learning.append(int(i))
instead doesn't help.
I then comment out the above block and attempt to read back the numbers
l=open('learning.txt','r')
learning=[]
for line in l:
print line.rstrip('\n')
This returns the list of numbers, but in the form 0.000000000e+00
, 1.000000000e+00
, etc, whereas I wanted them as integers.
If I instead type print int(line.rstrip('\n'))
, I get the error message invalid literal for int() with base 10: '0.0000000000000e+00'