Setting Encoding for Piped Output in Python
When piping stdout, the Python interpreter encounters an issue with encoding, leading to a UnicodeEncodeError. This occurs when a program attempts to print Unicode characters without specifying the correct encoding.
Best Practice: Encode Output When Piping
The solution is to manually encode the output before piping. A rule of thumb is to always work with Unicode internally, decoding input and encoding output. For example:
print(u"åäö".encode('utf-8'))
Decoding, Manipulating, and Encoding in a Pipe Sequence
A practical example is a Python program that converts between ISO-8859-1 and UTF-8, uppercase-ing the text:
import sys for line in sys.stdin: # Decode received input line = line.decode('iso8859-1') # Manipulate Unicode internally line = line.upper() # Encode output line = line.encode('utf-8') sys.stdout.write(line)
Avoid Setting Default Encoding
Modifying site.py or hardcoding the default encoding is not recommended. This can harm other modules that rely on the ASCII default. It's crucial to explicitly encode output when piping to ensure the desired results.
The above is the detailed content of How to Handle UnicodeEncodeError When Piping Output in Python?. For more information, please follow other related articles on the PHP Chinese website!