python中的文件输出给了我垃圾 [英] file output in python giving me garbage

查看:67
本文介绍了python中的文件输出给了我垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我写下面的代码时,我得到输出的垃圾.这只是一个查找质数的简单程序.当第一个for循环范围仅达到1000时有效,但是一旦范围变大,程序将无法输出有意义的数据

When I write the following code I get garbage for an output. It is just a simple program to find prime numbers. It works when the first for loops range only goes up to 1000 but once the range becomes large the program fail's to output meaningful data

output = open("output.dat", 'w')
for i in range(2, 10000):
    prime = 1
    for j in range(2, i-1): 
        if i%j == 0:
            prime = 0
            j = i-1
    if prime == 1:
        output.write(str(i) + " " )
output.close()
print "writing finished"

推荐答案

这是一个已知的记事本错误.结帐

This is a known Notepad bug. Check out

http://blogs.msdn.com/oldnewthing /archive/2007/04/17/2158334.aspx

触发此错误的经典方法是将布什隐藏事实"保存在文件中,然后保存,重新打开,然后对阴谋论大喊大叫,但我想"2 3 5 7 11 13 17"也可以奏效,除了您不会对阴谋论大喊大叫.

The classic way to trigger this bug is to put "Bush hid the facts" in a file, save it, reopen it, and scream about conspiracy theories, but I guess "2 3 5 7 11 13 17" works too, except that you don't get to scream about conspiracy theories.

这篇关于python中的文件输出给了我垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆