473,387 Members | 1,742 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

python33, UnicodeEncodeError, how to ignore error

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

Expand|Select|Wrap|Line Numbers
  1. fw=open(fname,'r',encoding='utf-8')
  2. bhtml=str(fw.readlines())
  3. soup=BeautifulSoup(bhtml)
  4. for tagdiv in soup.find_all("div", class_="hw_txt"):
  5.     word=tagdiv.get_text()
  6.     print(" replace word=",repr(word)) #gives error, interrupts script
  7.     print(" replace word=",word ).decode("utf-8","ignore")  #gives error, interrupts script
  8.     try:
  9.         print(" replace word=",word)
  10.     except IOError:
  11.         pass
  12.     #gives error, interrupts script
Feb 26 '15 #1
2 1557
bvdet
2,851 Expert Mod 2GB
Encapsulate the offending line in another try/except block.
Feb 26 '15 #2
dwblas
626 Expert 512MB
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)
Expand|Select|Wrap|Line Numbers
  1. import traceback
  2.  
  3. try:
  4.     fw=open(fname,'r',encoding='utf-8')
  5.     bhtml=str(fw.readlines())
  6.     soup=BeautifulSoup(bhtml)
  7.     for tagdiv in soup.find_all("div", class_="hw_txt"):
  8.         word=tagdiv.get_text()
  9.         print(" replace word=",repr(word))
  10.         print(" replace word=",word ).decode("utf-8","ignore")
  11.         print(" replace word=",word)
  12. except:
  13.     traceback.print_exc()
  14. ##    raise  ## uncomment to cause the program to stop on an error
Feb 26 '15 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Gateway | last post by:
Hi, I'm logged into a stand alone laptop as administrator and running MSDE for SQL 7 as a silent install using the standard unattend.iss and the command options -a -SMS -f1 unattend.iss The...
2
by: Trev | last post by:
Is there a way to make a SP ignore an error? e.g. I'm looping through each database on a server, checking of a table exists then selecting a value from that table. Now I have a database put onto...
5
by: Bob | last post by:
Hi Everybody I hope you can help. 2 related questions. 1. I am looking for a way to replace the confusing message box that comes up when a user trys to open a form without putting data in...
3
by: Matt F | last post by:
I'm having difficulty with a function and can't find what the problem is. The code is below. Essentially, it works fine if the target table (tbl variable in code) is empty, however if it contains...
2
by: nikosk | last post by:
I just spent a whole day trying to read an xml file and I got stuck with the following error: Exception Type: UnicodeEncodeError Exception Value: 'charmap' codec can't encode characters in...
1
by: Nimral | last post by:
Hi folks, is there a smart way to develop efficiently that helps me in the following situation: a sub calls a subsub, which calls a subsubsub, and so on. Within a subsubsub an error occurs...
1
by: time2hike | last post by:
Hi, I have a command button on my form that Closes the form and sends the user to another form Private Sub cmdGoToClientlkup_Click() 'Close Form DoCmd.Close 'return focus to Client...
2
by: aeristh | last post by:
Input: import pickle, shelve variety = shape = brand = f = open("pickles2.db", "wb") pickle.dump(variety, f) pickle.dump(shape, f)
0
by: jakov314 | last post by:
When I try to build my game compiler I get error: Traceback (most recent call last): File "gamecomp.py", line 22, in <module> executables = ) File...
1
by: gintare | last post by:
I am not able to initialize empty dictionary in python33. I have tried: D=dict() D={} Later i want to insert values using: D.setdefault(num).append(ansdb2.Rname) In both cases i am getting...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.