문제

I am debugging a complex issue and need to see the content of structured variable named context. Attempt to print it with this code fails:

print(repr(context))

With error message:

UnicodeEncodeError: 'charmap' codec can't encode character '\xb6' in position
2336: character maps to <undefined>

What is the reliable way to print structured variables to the screen for debug in Python 3?

도움이 되었습니까?

해결책

Use built-in ascii:

print(ascii(context))

It works similarly to repr in Python 2.

>>> ascii('\xb6')
"'\\xb6'"

>>> repr('\xb6')
"'¶'"
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top