-1

I am trying run a bash command from my python program which out put the result in a file.I am using os.system to execute the bash command.But I am getting an error as follows:

UnicodeEncodeError: 'ascii' codec can't encode character u'\u201c' in position 793: ordinal not in range(128)

I am not able to understand how to handle it.Please suggest me a solution for it.

akira
  • 521
  • 2
  • 5
  • 14

2 Answers2

0

Have a look at this Blog post

These messages usually means that you’re trying to either mix Unicode strings with 8-bit strings, or is trying to write Unicode strings to an output file or device that only handles ASCII.

Try to do the following to encode your string:

This can then be used to properly convert input data to Unicode. Assuming the string referred to by value is encoded as UTF-8: value = unicode(value, "utf-8")

T3 H40
  • 2,326
  • 8
  • 32
  • 43
  • I actually don’t have a string.What my bash command do is it prints the result in the terminal.As I am using os.system so I am able to get the result in a variable.so I am including a file name using ‘>>’ in the bash command itself to store the terminal contenant to a file .How can convert terminal content to utf 8 before printing it to the file? – akira Oct 29 '15 at 11:12
  • you can achieve this using `.encode()` - e.g. `print process.stdout.readline().encode("utf-8")` have a look [here](http://stackoverflow.com/questions/492483/setting-the-correct-encoding-when-piping-stdout-in-python) – T3 H40 Oct 29 '15 at 11:23
0

You need to encode your string as:

your_string =  your_string.encode('utf-8')

For example:

>>> print(u'\u201c'.encode('utf - 8'))
“
SuperBiasedMan
  • 9,814
  • 10
  • 45
  • 73
LetzerWille
  • 5,355
  • 4
  • 23
  • 26
  • I actually don’t have a string.What my bash command do is it prints the result in the terminal.As I am using os.system so I am able to get the result in a variable.so I am including a file name using ‘>>’ in the bash command itself to store the terminal contenant to a file .How can convert terminal content to utf 8 before printing it to the file? – akira Oct 29 '15 at 11:11
  • @akire your result is saved in a string I assume. Show please what you are trying to print to the terminal. Also to correctly display unicode chars you system default environment should be set to utf-8. Do print sys.getdefaultencoding() if output is not utf-8, you should find in help of you IDE how to change environment to utf-8 – LetzerWille Oct 29 '15 at 11:21