0

Here is my code:

#!/usr/bin/env python3.9
#-*- coding: big5 -*-
print("柯南")

It gives out an Error: SyntaxError: encoding problem: big5.
Why it gives out this Error when I actually has specified the coding?
I know by default python is using UTF-8 and it supports traditional chinese. It will fix it
But I really just want to play around with the syntax #-*- coding: encoding -*- and know why I get this error.

  • I don't think big5 is a valid encoding – drum Sep 14 '21 at 02:16
  • 1
    I read this document page https://docs.python.org/3/tutorial/interpreter.html#source-code-encoding and it references a list of codecs in https://docs.python.org/3/library/codecs.html#module-codecs. big5 is in the list. – random000 Sep 14 '21 at 02:18
  • 1
    Is your editor saving the file in that encoding? – solarc Sep 14 '21 at 02:20
  • I can reproduce the error by saving the file as UTF-8, like @solarc suggests. – wjandrea Sep 14 '21 at 02:20
  • no I will not get an error if I delete the coding line (using the default encoding scheme: UTF-8) or if I specify the coding scheme to be UTF-8. – random000 Sep 14 '21 at 02:24
  • I wonder does the supported language in the os will affect the encoding in python – random000 Sep 14 '21 at 02:28
  • Check [this answer](https://stackoverflow.com/questions/17182296/how-do-i-know-all-supported-encodings-recognized-by-pythons-unicode-function#17182324) for the list of encodings supported. `big5` seems to show as `big5_tw` or `csbig5` so try those. – solarc Sep 14 '21 at 02:30
  • Are you essentially trying to decode a UTF-8-encoded file with BIG5? – blhsing Sep 14 '21 at 02:31
  • @blhsing sorry what do you mean? – random000 Sep 14 '21 at 02:45
  • @solarc big5_tw and csbig5 also does not work – random000 Sep 14 '21 at 02:47
  • 1
    I mean your Python source code was likely saved with UTF-8 encoding, but with the header specifying an incorrect encoding of BIG5, the file can't be decoded properly as a result. – blhsing Sep 14 '21 at 02:47
  • 1
    @bihsing oh yes I try this `mint@vivobook:~/Documents$ file test.py` It gives me the result: `test.py: Python script, UTF-8 Unicode text executable`. Then how can i change the python script into big5 executable? – random000 Sep 14 '21 at 06:13
  • Does this answer your question? [Best way to convert text files between character sets?](https://stackoverflow.com/questions/64860/best-way-to-convert-text-files-between-character-sets) – wjandrea Sep 14 '21 at 12:26

0 Answers0