문제

I have a regex substitution for the character -, replacing it with ». That all works just fine, however, when outputting the substituted result it is escaped. How do I properly print the characters upon output?

#!/usr/bin/python
# coding: utf-8
# -*- coding: utf-8 -*- 
import os, sys
import re

searchText = "SKY ROCKETS IN FLIGHT - AFTERNOON DELIGHT"
result = re.sub("(\\-)", "»", searchText)
resultdecoded = result.decode('string_escape') 
print("output:", resultdecoded)

('output:', 'SKY ROCKETS IN FLIGHT \xc2\xbb AFTERNOON DELIGHT')

도움이 되었습니까?

해결책

In Python 3, where print is a function, this would generate the correct output.

In Python 2, where print is a statement, you're not printing two different objects - you're printing a single tuple, created by putting the items in parentheses with a comma between them (,). The string representation of a tuple tries to show how that string would look in the program.

The fix is to take off the parentheses.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top