Read and Print ASCII >128 as is in cp437 on DOT-MATRIX printer or file regardless local setting

Sjoerd Mullender sjoerd.mullender at oratrix.com
Thu Dec 13 10:41:40 EST 2001


First of all, *by definition* there is no such thing as a ASCII
character > 128.  ASCII is a 7-bit character set, so values range from
0 to 127.

So, you must determine which character set you actually want to
convert to.  If it is ASCII, fine, but then you won't be able to
convert any non-ASCII characters (e.g. accented characters).

The short answer is: use

unicode(data, 'cp437').encode('us-ascii')

where data is the text that you want to convert.
Or if you want to convert to ISO 8859-2, use
unicode(data, 'cp437').encode('iso-8859-2')

However, odds are that you will get an error because the source data
cannot be converted to the destination formt.

So, an alternative which will ignore all unconvertible characters is:

result = []
for c in unicode(data, 'cp437'):
    try:
        result.append(c.encode('iso-8859-2')) # or 'us-ascii', or ...
    except:
        pass
print ''.join(result)

On Thu, Dec 13 2001 Hrvoje_Tereek wrote:

> Pleas could You help me about my problem?
> 
> 
> 
> Summary:    Read and Print ASCII >128 as is in cp437 regardless local
> setting !
> 
> System:       Win2000, Linux ; locals-cp1250
> 
> Status:         URGENT
> 
> 
> 
> Content:
> 
> I have to wrote report generator. For this purpose must be able to read
> and print ASCII characters > 128. Not to mention that I need characters
> from US ASCII found in Pithon21/Lib/encodings/cp437.py. I'd tried all
> encode, decode, codecs trick I have found on Internet but always the
> same. ' ASCII is bigger then 128 !!!'.
> 
> Also I have print template file written with Notepad (witch can exactly
> reproduce characters such ALT+218) witch I read, manipulate end print
> from python. I couldn't trick python to not encode read characters such
> ALT+218 in same utf-8 three characters for any ASCII > 128.
> 
> Yes it must be something with locale settings or else but isn't it some
> easy-normal way.
> 
> 
> 
> Thanks, at front
> 
> 
> 
> Yours Python fan,
> 
> Hrvoje Tercek
> 

-- Sjoerd Mullender <sjoerd.mullender at oratrix.com>




More information about the Python-list mailing list