转换为字符串或写入文件时更改为整数值

问题描述

我的简单素数生成器有一个小问题。

免责声明:我正在使用数量惊人的数字-现在超过2,000,000位数,这可能会导致python不稳定,但是我希望这是一个可以解决的已知问题。

f = open('primes.txt','r')          #this is a file containing all prime numbers less than n 
                                     #sequentially. I have another program that finds and adds
                                     #more prime numbers to this file.

i = 1
for line in f:                       #Since the primes are separated by line breaks,I take the int of 
                                     #each line and multiply them together and stores the massive number 
                                     #in a variable called i 
    i = i*int(line)
f.close()
i = i+1                              #since I know that every prime up to a certain point is a factor of
                                     #i,I can add or subtract 1 to guarantee a number that has no prime
                                     #factors (and therefore no factors) 

print(i)                             #prints i to the terminal followed by 3 linebreaks
print("\n\n\n")


h = str(i)                           #I want to ultimately write i to a text file,but the write command
                                     #doesn't take integer inputs

print(len(h))                        #prints how large my new prime is

f = open('largestprime.txt','w')    #opens a new file and overwrites what's currently inside

if int(h) == i:                      #I wrote this statement as a check when I started noticing something 
                                     #unusual was going on. It basically checks to make sure that the 
                                     #string h is the same thing as the integer i before writing to file.
    f.write(h)
else:
    f.write("Something Hinky's Going On")
    

input()                             #Waits for an enter before closing the terminal

对我来说似乎很简单,但是每次打印到屏幕上的数字和写入文件的数字都不相同。这是最新的示例:

-终端打印:

 6690896934...(2,009,810 other digits)...0355781011 (total of 2,830 digits)

-文件现在包含:

 5562116702...(2,007,021 other digits)...6916126637 (total of 2,041 digits)

文件丢失了2,000多位数字! 我在写入文件时犯了一些错误?将荒谬的长字符串写入文件时,python中是否存在已知的错误?

任何帮助将不胜感激。谢谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...