Printing characters outside of the ASCII range

danielk danielkleinad at gmail.com
Fri Nov 9 16:46:42 EST 2012


On Friday, November 9, 2012 4:34:19 PM UTC-5, Prasad, Ramit wrote:
> 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.

D:\home\python>pytest.py
Traceback (most recent call last):
  File "D:\home\python\pytest.py", line 1, in <module>
    print(chr(253).decode('latin1'))
AttributeError: 'str' object has no attribute 'decode'

Do I need to import something?



More information about the Python-list mailing list