I want to write a really large integer from Python to a textfile, about 10 to 1000 Megabyte.
The following options have the same speed, unfortunately both are really slow:
import time
import pickle
num = 17**(10**7)
t1=time.time()
pickle.dump( num , open( "save2.p", "wb" ) )
t2=time.time()
print(str(t2-t1))
t3=time.time()
file = open("testfile2.txt","w")
file.write(str(num))
file.close()
t4=time.time()
print(str(t4-t3))
(of course, the value of num
is just a placeholder for another big integer)
My questions:
Who can help?
Try this:
import random
f=open("filename.txt","w")
char=""
ints=["0","1","2","3","4","5","6","7","8","9"]
for i in range(10**10): #Ten to the power of ten
char+=random.choice(ints)
if char.startswith("0"):
char=char[1:]
f.write(char)
f.close()
If you want to read the integer, you write this:
myInt=int(open("filename.txt","r").read())
Hope this helps!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.