Skip to content Skip to sidebar Skip to footer

Why Is Python Writing Out In Chinese Characters?

This is my first question on Stack Overflow and so I want to apologise first if my question is not formatted correctly. I am not particularly experienced with coding, but am trying

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:

  1. Installed python3
  2. Re-saved both of the .txt files as Unicode UTF-8 with Unix(LF) line breaks.
  3. Changed "#!/usr/bin/env python" to "#!/usr/bin/env python3" at the beginning of the script.
  4. 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?"