A document contains text and notes below it. Both sections contains unicode. I want to add a disclaimer above the notelist.
[CODE=python]import re
f = open('C:/example.txt', 'r')
source = f.read()
patt = re.compile(r'(. *)[a-zA-Z\-]+\s?(?<![Error]):[^\[\]\n]*', re.DOTALL) # finding the main text
text= patt.search(sou rce) # returns text containing unicode
patt = re.compile(r'[a-zA-Z\-]+\s?(?<![Error]):[^\[\]\n]*') # finding the notes
notelist = patt.findall(so urce) # returns a list of notes, containing more unicode
notes = u'\n'.join(str( x) for x in notelist) # converting the list to a string
outputFile = open('C:/example.txt', 'w')
outputFile.writ e(u'%s%s\n\n%s' % (text, 'This is a disclaimer above the list of notes', notes)) # writes the orignal text to the original file with the disclaimer added[/CODE]
The first error here comes in point 8 where the list 'notelist' is converted to a string. The second one appears at point 10 where 'text' contains unicode characters that cannot be decoded.
[CODE=python]import re
f = open('C:/example.txt', 'r')
source = f.read()
patt = re.compile(r'(. *)[a-zA-Z\-]+\s?(?<![Error]):[^\[\]\n]*', re.DOTALL) # finding the main text
text= patt.search(sou rce) # returns text containing unicode
patt = re.compile(r'[a-zA-Z\-]+\s?(?<![Error]):[^\[\]\n]*') # finding the notes
notelist = patt.findall(so urce) # returns a list of notes, containing more unicode
notes = u'\n'.join(str( x) for x in notelist) # converting the list to a string
outputFile = open('C:/example.txt', 'w')
outputFile.writ e(u'%s%s\n\n%s' % (text, 'This is a disclaimer above the list of notes', notes)) # writes the orignal text to the original file with the disclaimer added[/CODE]
The first error here comes in point 8 where the list 'notelist' is converted to a string. The second one appears at point 10 where 'text' contains unicode characters that cannot be decoded.
Comment