简体   繁体   中英

Python 3.x: Ensure a string is ready for printing

This is something that drove me crazy today while writing large amounts of text to files or stdout. I would often get error messages about bad characters. Here's a simplified test case:

Python 3.4.0b2 (v3.4.0b2:ba32913eb13e, Jan  5 2014, 16:13:26) [MSC v.1600 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> x='\x92'
>>> print(x)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Python34\Lib\encodings\cp437.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_map)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\x92' in position 0: character maps to <undefined>

I have a big chunk of text. I want to print it all or save to file, but without getting this bug. Is it possible? My preferred solution would be to save this annoying character (are there more?) to file, but if there's a nice way to filter it, that'll be nice too (I know how to filter a single character,but I don't know whether '\\x92' has a bunch of similar characters to it that also need to be filtered.)

sys.stdout周围创建一个使用backslashreplace codecs.StreamWriter错误处理程序的codecs.StreamWriter ,并将其作为print()file参数传递。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM