Where possible, I want to use the unicode python string u"\\U0001d54d"
but if there will be problems displaying it, I would like to use just a "V".
On some system the unicode prints. On others it displays nothing (I assume there is a .encode("ascii","ignore")
type thing happening) or I get
UnicodeEncodeError: 'ascii' codec can't encode character u'\U0001d54d' in position 14: ordinal not in range(128)
depending on the function... both are bad.
Is there a test I can do to determine whether or not to use my special character? or is it more complicated than that?
The displaying part depends on where you are going to print. Python encodes the output to whatever encoding your terminal application is using. You can check the environment vars to be sure that the chars are defined in the locale you need, for example:
import os
if os.environ.get('LC_ALL') == 'es_ES.utf8':
# You know that 'es_ES.utf8' has your character ...
Also check LC_CTYPE
You can easily check what encoding your stdout supports:
>>> import sys
>>> sys.stdout.encoding
'UTF-8'
Quoting the Python Glossary,
Easier to ask for forgiveness than permission :
Easier to ask for forgiveness than permission. This common Python coding style assumes the existence of valid keys or attributes and catches exceptions if the assumption proves false. This clean and fast style is characterized by the presence of many try and except statements. The technique contrasts with the LBYL style common to many other languages such as C.
The standard way of handling runtime errors in Python is try\\except. try
printing your string, and except UnicodeEncodeError
to fallback in case of error.
>>> try:
... s=u"\U0001d54d"
... print s
... except UnicodeEncodeError:
... print "V"
...
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.