Printing characters outside of the ASCII range

Prasad, Ramit ramit.prasad at jpmorgan.com
Fri Nov 9 16:34:01 EST 2012


danielk wrote:
> 
> The database I'm using stores information as a 3-dimensional array. The delimiters between elements are
> chr(252), chr(253) and chr(254). So a record can look like this (example only uses one of the delimiters for
> simplicity):
> 
> name + chr(254) + address + chr(254) + city + chr(254) + st + chr(254) + zip
> 
> The other delimiters can be embedded within each field. For example, if there were multiple addresses for 'name'
> then the 'address' field would look like this:
> 
> addr1 + chr(253) + addr2 + chr(253) + addr3 + etc ...
> 
> I use Python to connect to the database using subprocess.Popen to run a server process. Python requests
> 'actions' like 'read' and 'write' to the server process, whereby the server process performs the actions. Some
> actions require that the server send back information in the form of records that contain those delimiters.
> 
> I have __str__ and __repr__ methods in the classes but Python is choking on those characters. Surely, I could
> convert those characters on the server before sending them to Python and that is what I'm probably going to do,
> so guess I've answered my own question. On Python 2, it just printed the 'extended' ASCII representation.
> 
> I guess the question I have is: How do you tell Python to use a specific encoding for 'print' statements when I
> know there will be characters outside of the ASCII range of 0-127?

You just need to change the string to one that is not 
trying to use the ASCII codec when printing. 

print(chr(253).decode('latin1')) # change latin1 to your 
                                 # chosen encoding.
ý


~Ramit


This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.  



More information about the Python-list mailing list