Question

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')

Was it helpful?

Solution

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top