简体   繁体   中英

why does python write to a file in gibberish characters

I attempted Problem 10 at project euler and passed but I decided, what if i wote all the prime numbers below 2 million to a text(.txt) file and so I continued and so made some small adjustments to the main function which solved the problem so without just adding it to a variable(tot) I wrote the prime number which was generated by a generator to a text file and it at first worked but forgot to add spaces after each prime number, so the output was sort of gibberish

357111317192329313741434753

so I modified my txt.write(str(next_prime)) to txt.write(str(next_prime) + ' ')

after that slight modification, the output was completely gibberish

″‵‷ㄱㄠ″㜱ㄠ‹㌲㈠‹ㄳ㌠‷ㄴ㐠″

here's my complete code for the function:

def solve_number_10():
    total = 2
    txt = open("output.txt","w")
    for next_prime in get_primes(3):
        if next_prime < 2000000:
            txt.write(str(next_prime) + ' ')
            #total += next_prime
        else:
            print "Data written to txt file"
            #print total
            txt.close()
            return

Why does this happen and how could I make the output like

3 5 7 11 13 17 19

This is a bug in Microsoft's Notepad program, not in your code.

>>> a = '‵‷ㄱㄠ″㜱ㄠ‹㌲㈠‹ㄳ㌠‷ㄴ㐠'
>>> a.decode('UTF-8').encode('UTF-16LE')
'5 7 11 13 17 19 23 29 31 37 41 4'

Oh hey, look, they're prime numbers (I assume 4 is just a truncated 43).

You can work around the bug in Notepad by

  1. Using a different file viewer that doesn't have the bug.

  2. Write a ZWNBSP, once, to the beginning of the file, encoded in UTF-8:

     txt.write(u'\'.encode('UTF-8')) 

    This is incorrectly called a BOM. It would be a BOM in UTF-16, but UTF-8 is not technically supposed to have a BOM. Most programs ignore it, and in other programs it will be harmless.

Try this:

txt.write('%i ' % next_prime)

Looks like str() is converting your number to a character that matches it in some encoding, and not to its string representation.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM