Why Is Python Writing Out In Chinese Characters?
Solution 1:
It seems Python supports an "encoding" parameter in the open() function to override the default encoding format. Provided you know what the correct encoding for your input and output files are, you should be able to correct it by adding something like the following (replacing the actual encodings with the correct ones in your case):
newnames= open('newheaders.txt','r', encoding='ascii')
newfasta= open('newfasta.txt', 'w', encoding='utf_8')
PS: Seems like the problem is due to Python 3 using Unicode by default for text file I/O, which is a change from Python 2.x.
Solution 2:
Thank you for your help, everybody. It's now resolved (Essentially I am an idiot)...
How I fixed it:
- Installed python3
- Re-saved both of the .txt files as Unicode UTF-8 with Unix(LF) line breaks.
- Changed "#!/usr/bin/env python" to "#!/usr/bin/env python3" at the beginning of the script.
- Ran python3 /Users/Sophie/Desktop/AttemptToRename/replacenames.py from the command line.
And it worked!
I'm not sure if all of these steps or only some of them were necessary, but it's now working as planned. Thanks again for all your help. Gonna go through and up-vote now! [Edit: apparently my up-votes don't show because I have a low reputation... :/]
Post a Comment for "Why Is Python Writing Out In Chinese Characters?"