1

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

ctfd
  • 338
  • 3
  • 14
  • 2
    Take the parentheses off your `print`. This is obviously Python 2, not Python 3. – Mark Ransom Mar 31 '14 at 18:29
  • @MarkRansom Wow, that was easy (and yes it's python 2). If you put that as an answer i'll gladly accept it. thank you :) – ctfd Mar 31 '14 at 18:32

1 Answers1

1

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.

Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • One other question, what is the difference between `# coding: utf-8` and `# -*- coding: utf-8 -*-` — is using one or both more correct than the other? – ctfd Mar 31 '14 at 20:24
  • @ctfd they'll both tell Python that your source file is utf-8. The one with the extra characters works with Emacs too, see http://stackoverflow.com/questions/4872007/where-does-this-come-from-coding-utf-8 – Mark Ransom Mar 31 '14 at 20:48