1

Im new in python I'm trying to print some chineses word to command line windows 10 and file but got a problem:

Error messages

Here is my code:

fh = open("hello.txt", "w") 
str="欢迎大家加入自由职业者群体。谢谢大家"
print(str)
fh.write(str) 
fh.close()
Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
Quyet An
  • 11
  • 2
  • Perhaps the problem is your indentation. Python is very whitespace sensitive. Try un-indenting all of your code. – Christian Dean Apr 25 '17 at 15:30
  • i change it but still error, anyone have an example print this word to cmd line? – Quyet An Apr 25 '17 at 15:37
  • Your error message was pretty helpful and explicit - in the future, try googling with the text of the error message you get and you'll probably find others with the same problem. Which is how I found [this post](http://stackoverflow.com/questions/27092833/unicodeencodeerror-charmap-codec-cant-encode-characters) related to your problem – HFBrowning Apr 25 '17 at 16:29
  • @QuyetAn - Be sure to use coding: (as mentioned below) to set the encoding of your Python source file. UTF-8, Big5, EUC, etc... – lit Apr 26 '17 at 15:32

3 Answers3

1

The default encoding of files is locale.getpreferredencoding(False), which seems to be cp1252 on your system. Specify the encoding when opening the file.

Also use with and the file will be closed for you when it exits the block:

#!python3.6
with open('hello.txt','w',encoding='utf8') as fh:
    str="欢迎大家加入自由职业者群体。谢谢大家"
    print(str)
    fh.write(str) 

To see the Chinese characters on the console you'll need to install a Chinese language pack, and change the console font to one that supports Chinese. Using an IDE that supports UTF-8 will also work. The "boxed question mark" characters are what is displayed when the font doesn't support the characters. If you cut-n-paste those characters to an application like Notepad that has support for Chinese fonts you should see the correct characters.

Here's my US Windows system with the Chinese Language Pack. The console is configured with the SimHei font.

enter image description here

Mark Tolonen
  • 166,664
  • 26
  • 169
  • 251
0

Couple of issues:

There shouldn't be an identation after the fh variable declaration. You shouldn't name a string "str" because that's a builtin function. If you want to use characters outside the latin alphabet you need to declare that you're using UTF-8 like so: # -*- coding: utf-8 -*- (put that at the top of your file). Then it should work. Although terminal does sometimes have issues with foreign characters.

# -*- coding: utf-8 -*-

fh = open("hello.txt", "w")
str1="欢迎大家加入自由职业者群体。谢谢大家"
print(str1)
fh.write(str1)
fh.close()

Edit

Official solution is, use PyCharm!

TLOwater
  • 638
  • 3
  • 13
  • still error File "C:\Users\zangq\AppData\Local\Programs\Python\Python36-32\lib\encodings\cp1252.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_table)[0] UnicodeEncodeError: 'charmap' codec can't encode characters in position 0-17: character maps to – Quyet An Apr 25 '17 at 15:36
  • @QuyetAn Which OS are you running this on? Although you have some mistakes in your code, it still works for me on Ubuntu 16.4. – Christian Dean Apr 25 '17 at 15:39
  • @ChristianDean It's windows based on his file path. That's what I'm using and it's fine. – TLOwater Apr 25 '17 at 15:40
  • @QuyetAn Can you open your program in the python IDLE and run it from there. Command line can be problematic – TLOwater Apr 25 '17 at 15:42
  • @ChristianDean im using windows 10 – Quyet An Apr 25 '17 at 15:47
  • @QuyetAn Ok brilliant, pycharm is very flexible it's where I originally tested and it always works. – TLOwater Apr 25 '17 at 15:49
  • @TLOwater Don’t assume `open` default encoding is UTF-8. On Windows it isn’t. Use the `encoding` parameter. Also when declaring the encoding of the source file with `#!coding=utf8` (the default on Python 3) it’s good to remind the coder to save the file in that encoding as well (also not usually the default on Windows). – Mark Tolonen Oct 07 '19 at 14:30
0

For me, changing the font in command line worked.
System: Windows 10
Font that worked: NSimSun

SherylHohman
  • 16,580
  • 17
  • 88
  • 94
muuba
  • 1
  • 1
    Your answer is currently lacking in detail. Can you please expand upon it, such that future readers, or in fact the OP, can perform the suggestion you've offered as a solution. – Compo May 08 '20 at 14:59
  • While this setting may resolve the OP's issue, it is best to include an explanation as to why it solves the OP's issue. In this way, future visitors can learn from your post, and apply it to their own code. SO is not a coding service, but a resource for knowledge. Also, high quality, complete answers are more likely to be upvoted. These features, along with the requirement that all posts are self-contained, are some of the strengths of SO as a platform, that differentiates it from forums. You can `edit` to add additional info & to supplement your explanations with source documentation. – SherylHohman May 08 '20 at 19:37