简体   繁体   中英

Python--output file has Chinese characters

My goal is to simply have +1 and -1 in my output.txt file, yet I keep getting Chinese characters

ㄭㄫㄫㄫㄫㄭ

The code snippet below works fine in a separate Python file:

f = open('C:\\Users\\Max\\Downloads\\output.txt', 'w', encoding='ascii')
f.write('+1')
f.write('-1')

Yet generates Chinese characters when I put it into another project:

if(algorithm==1):
     f.write('+1')
else:
     f.write('-1')

The code is successfully going into the if/else statements (debugged). Any idea on what's wrong?

Looked at: Why is python writing out in Chinese characters?

Used

encoding='cp1252'

And it printed out successfully without having to use '\\n'. Caught it when PyCharm listed it on the debugger while debugging.

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