Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python Print/Write String containing "\f"

Tags:

python

string

I am trying to read a file and replace every "a ... a" by a '\footnotemark'

with open('myfile', 'r') as myfile:
   data = myfile.read()
   data = re.sub('<a.+?</a>', '\footnotemark', data)

Somehow Python always makes '\footnotemark' to '\x0cootnotemark' ('\f' to '\x0c'). I tried so far

  • Escaping: '{2 Backslashes}footnotemark'
  • raw String: r'\footnotemark' or r'"\footnotemark"'

None of these worked

Example input:

foo<a href="anything">asdasd</a> bar

Example output:

foo\footnotemark bar
like image 854
enzian Avatar asked Jan 28 '26 10:01

enzian


1 Answers

Assuming Python2 since You haven't mentioned anything about version

#/usr/bin/python

import re

# myfile is saved with utf-8 encoding
with open('myfile', 'r') as myfile:

    text = myfile.read()
    print text
    data = re.sub('<a.+?</a>', r'\\footnotemark', text)

print data

outputs

foo<a href="anything">asdasd</a> bar
foo\footnotemark bar
like image 177
kivy_student Avatar answered Jan 30 '26 23:01

kivy_student



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!