Unicode, lists & strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nihilium
    New Member
    • Mar 2008
    • 16

    #1

    Unicode, lists & strings

    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.
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    Originally posted by nihilium
    [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.
    What are your errors?

    On first inspection I'd say add [] around your list comprehension on line 8, ie:
    [code=python]notes = u'\n'.join([str(x) for x in notelist]) # converting the list to a string[/code]

    Comment

    • nihilium
      New Member
      • Mar 2008
      • 16

      #3
      Originally posted by jlm699
      What are your errors?

      On first inspection I'd say add [] around your list comprehension on line 8, ie:
      [code=python]notes = u'\n'.join([str(x) for x in notelist]) # converting the list to a string[/code]
      It turns out my problems do not lie where I thought they did. I'll have to do more research; thanks for the help.

      Comment

      Working...