Skip to main content
Stack Overflow
  1. About
  2. For Teams

Return to Question

added 39 characters in body
Source Link
Maddy
  • 1.4k
  • 4
  • 25
  • 43

I'm trying to encode non-ascii characters in python using utf-16-le, and here's the snippet of the code for this:

import os
import sys
def run():
 print sys.getdefaultencoding()
 reload(sys)
 sys.setdefaultencoding('utf-16-le')
 print sys.getdefaultencoding()
 test_dir = unit_test_utils.get_test_dir("utkarsh")
 dir_name_1 = '東京'
 ....
 ....
if __name__ == '__main__':
 run()

When this code is run, this is the error seen:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf 
 File "/root/python/tests/abc.py", line 27
SyntaxError: Non-ASCII character '\xe6' in file /root/python/tests/abc.py on line 27, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How can this be fixed? I tried adding this line to the beginning of the file, but to no avail:

# -*- coding: utf-16-le -*-

The error this time around was:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf
 File "/root/python/tests/abc.py", line 2
 import os
import sys
...
...
if __name__ == '__main__':
 run()
 ^
SyntaxError: invalid syntax

Edit:

Line 27: dir_name_1 = '東京'

I'm trying to encode non-ascii characters in python using utf-16-le, and here's the snippet of the code for this:

import os
import sys
def run():
 print sys.getdefaultencoding()
 reload(sys)
 sys.setdefaultencoding('utf-16-le')
 print sys.getdefaultencoding()
 test_dir = unit_test_utils.get_test_dir("utkarsh")
 dir_name_1 = '東京'
 ....
 ....
if __name__ == '__main__':
 run()

When this code is run, this is the error seen:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf 
 File "/root/python/tests/abc.py", line 27
SyntaxError: Non-ASCII character '\xe6' in file /root/python/tests/abc.py on line 27, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How can this be fixed? I tried adding this line to the beginning of the file, but to no avail:

# -*- coding: utf-16-le -*-

The error this time around was:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf
 File "/root/python/tests/abc.py", line 2
 import os
import sys
...
...
if __name__ == '__main__':
 run()
 ^
SyntaxError: invalid syntax

I'm trying to encode non-ascii characters in python using utf-16-le, and here's the snippet of the code for this:

import os
import sys
def run():
 print sys.getdefaultencoding()
 reload(sys)
 sys.setdefaultencoding('utf-16-le')
 print sys.getdefaultencoding()
 test_dir = unit_test_utils.get_test_dir("utkarsh")
 dir_name_1 = '東京'
 ....
 ....
if __name__ == '__main__':
 run()

When this code is run, this is the error seen:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf 
 File "/root/python/tests/abc.py", line 27
SyntaxError: Non-ASCII character '\xe6' in file /root/python/tests/abc.py on line 27, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How can this be fixed? I tried adding this line to the beginning of the file, but to no avail:

# -*- coding: utf-16-le -*-

The error this time around was:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf
 File "/root/python/tests/abc.py", line 2
 import os
import sys
...
...
if __name__ == '__main__':
 run()
 ^
SyntaxError: invalid syntax

Edit:

Line 27: dir_name_1 = '東京'

Source Link
Maddy
  • 1.4k
  • 4
  • 25
  • 43

Using UTF-16 encoding in python

I'm trying to encode non-ascii characters in python using utf-16-le, and here's the snippet of the code for this:

import os
import sys
def run():
 print sys.getdefaultencoding()
 reload(sys)
 sys.setdefaultencoding('utf-16-le')
 print sys.getdefaultencoding()
 test_dir = unit_test_utils.get_test_dir("utkarsh")
 dir_name_1 = '東京'
 ....
 ....
if __name__ == '__main__':
 run()

When this code is run, this is the error seen:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf 
 File "/root/python/tests/abc.py", line 27
SyntaxError: Non-ASCII character '\xe6' in file /root/python/tests/abc.py on line 27, but no encoding declared; see http://www.python.org/peps/pep-0263.html for details

How can this be fixed? I tried adding this line to the beginning of the file, but to no avail:

# -*- coding: utf-16-le -*-

The error this time around was:

# /u/bin/python-qs /root/python/tests/abc.py -c /root/test.conf
 File "/root/python/tests/abc.py", line 2
 import os
import sys
...
...
if __name__ == '__main__':
 run()
 ^
SyntaxError: invalid syntax
lang-py

AltStyle によって変換されたページ (->オリジナル) /