python33, UnicodeEncodeError, how to ignore error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • gintare
    New Member
    • Mar 2007
    • 103

    python33, UnicodeEncodeError, how to ignore error

    I want that my script would continue running despite the error.

    Code:
    fw=open(fname,'r',encoding='utf-8')
    bhtml=str(fw.readlines())
    soup=BeautifulSoup(bhtml)
    for tagdiv in soup.find_all("div", class_="hw_txt"):
        word=tagdiv.get_text()
        print(" replace word=",repr(word)) #gives error, interrupts script
        print(" replace word=",word ).decode("utf-8","ignore")  #gives error, interrupts script
        try:
            print(" replace word=",word)
        except IOError:
            pass
        #gives error, interrupts script
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    Encapsulate the offending line in another try/except block.

    Comment

    • dwblas
      Recognized Expert Contributor
      • May 2008
      • 626

      #3
      The first 2 print statements will yield an error because the are not in a try/except. The last print statement will error out for anything other than an IO error. Putting the entire block in a try except will let the code run but may or may not allow you to trace the errors.

      Also see this link for the correct use of decode http://www.tutorialspoint.com/python/string_decode.htm (decoding the return value from a print function will not yield anythng useful)
      Code:
      import traceback
      
      try:
          fw=open(fname,'r',encoding='utf-8')
          bhtml=str(fw.readlines())
          soup=BeautifulSoup(bhtml)
          for tagdiv in soup.find_all("div", class_="hw_txt"):
              word=tagdiv.get_text()
              print(" replace word=",repr(word))
              print(" replace word=",word ).decode("utf-8","ignore")
              print(" replace word=",word)
      except:
          traceback.print_exc()
      ##    raise  ## uncomment to cause the program to stop on an error

      Comment

      Working...