473,385 Members | 1,185 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,385 software developers and data experts.

from WWW through urllib2 to MySQL --> encoding fuss

Hi all,
I've spent last few hours trying to figure out the following:
  1. fetch a website using urllib2
  2. filter out some of its content using SGMLParser
  3. decode result from p. 2. using charset as set in <META http-equiv="Content-Type" ...> of the website
  4. encode the whole thing using UTF-8
  5. stuff it into MySQL
Here it dies with the following:
Expand|Select|Wrap|Line Numbers
  1.  File "c:\python24\lib\site-packages\MySQLdb\cursors.py", line 147, in execute
  2.  query = query.encode(charset)
  3. UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 8584: ordinal not in range (128)
  4.  
First of all, I have no idea why it's trying to use 'ascii' codec in the first place, as charset in MySQL is set to 'latin1': it's fetched properly by MySQLdb/cursors.py - that's checked.
Secondly, there seems to be little change no matter if I do any decode/encode or not in points 3 and 4 above. It just won't go.
Right now I'm on the verge of claiming that Python's decode() doesn't do its job. As an example:
Expand|Select|Wrap|Line Numbers
  1. websiteContent.decode('iso-8859-1').encode('ascii')
  2. UnicodeEncodeError: 'ascii' codec can't encode character u'\x97' in position 8033: ordinal not in range(128)
  3.  
won't go, but when I'm encode()ing using 'utf8' it works, failing only as described above, when I put the result into SQL query.

Any hints on how to do the 1-2-3-4-5 sequence above will be appreciated.
PTB
Nov 2 '06 #1
2 4415
Not sure what the etiquette is on replying to a month-old topic...

1. If you do a s.encode('latin-1') and s is not a unicode object, python will first try to convert the string to unicode using the default encoding, which is ascii. That's why you got the first error. To resolve this, first decode the query you have using the unicode() or decode() functions.

2. When you try to encode into a specific charset, but the original string contains characters which aren't supported by this charset, you're going to get an exception. You can instruct python to ignore errors by using u.encode('ascii', 'ignore')

Expand|Select|Wrap|Line Numbers
  1. >>> s = "hello\x97"
  2.  
  3. >>> s.encode('latin-1')
  4. Traceback (most recent call last):
  5.   File "<stdin>", line 1, in <module>
  6. UnicodeDecodeError: 'ascii' codec can't decode byte 0x97 in position 5: ordinal not in range(128)
  7.  
  8. >>> s.decode('iso-8859-1')
  9. u'hello\x97'
  10.  
  11. >>> s.decode('iso-8859-1').encode('latin-1')
  12. 'hello\x97'
  13.  
  14. >>> s.decode('iso-8859-1').encode('ascii')
  15. Traceback (most recent call last):
  16.   File "<stdin>", line 1, in <module>
  17. UnicodeEncodeError: 'ascii' codec can't encode character u'\x97' in position 5: ordinal not in range(128)
  18.  
  19. >>> s.decode('iso-8859-1').encode('ascii', 'ignore')
  20. 'hello'
  21.  
Nov 24 '06 #2
bartonc
6,596 Expert 4TB
Not sure what the etiquette is on replying to a month-old topic...
Welcome to TheScripts. Thanks for posting. I say "if it's not a closed thread, post away!". This is very useful info for all to see and we appreciate your input.
Drop in anytime,
Barton (python forum moderator)
Nov 24 '06 #3

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

Similar topics

2
by: Mark | last post by:
I have found many examples of how to access an HTML text field from PHP, but cannot find the reverse - how to load an HTML form from a MySQL database. Specifically, I have an HTML form which...
4
by: Bob | last post by:
Hi, I am having trouble registering a row from a mysql query so far I have the mysql part working : while ( $val = mysql_fetch_array( $result )) { print("<tr><td>$val</td> <td>$val</td>...
4
by: Bryan Powell | last post by:
Is it possible to grab data from a MySQL database and print it on the screen with VC++? The database is on a linux server if that would make any difference.
0
by: IamtheEvster | last post by:
Hi All, I am currently using PHP 5 and MySQL 5, both on Fedora Core 5. I am unable to call a MySQL stored procedure that returns output parameters using mysql, mysqli, or PDO. I'm having a...
3
by: Alina Ghergu | last post by:
Hi there, I'm currently developing a GUI for a network monitor. I have to plot data taken from a MySQL database. For GUI I use wxPython, for plotting Matplotlib. The plotting has to be realtime,...
4
by: Richard | last post by:
hi there, i've got a mysql database with a research data. i would like to get some information from that database and save it as ms access database. it will be a client feature. the client...
3
by: John Nagle | last post by:
I'm getting a wierd error from urllib2 when opening certain URLs. The code works for most sites, but not all of them. Here's the traceback: InfoSitePage EXCEPTION while processing page...
2
by: s2c2 | last post by:
Hello. sorry for my poor english. i try to make a select from 2 mysql servers located in diferents machines. i call a mysql stored procedure from java app and i want join a select from table...
3
by: Vik Rubenfeld | last post by:
I have to search 2 mySQL tables, and show the user a single sorted list that contains all the results from both mySQL queries. My question is, how do you get all the resulting items from both...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
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: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: 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...

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.