473,395 Members | 2,437 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,395 software developers and data experts.

MySQL: 'latin-1' codec can't encode character

Hi.

I'm trying to store a text within a MySQL field (v 3.23.58) by using
MySQLdb
(v 1.2.1c3).

The text is: "telephone..." (note the last character)

And I get this error message:
-----------
File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line
33, in defaulterrorhandler
raise errorclass, errorvalue
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2026' in
position 288: ordinal not in range(256)
-----------------------

Position 288 is the character I've mentioned. I suppose I must encode
this caracter
into a right one which MySQL could store, but I have no idea about how
to perform
it. Any suggestion?

Thank you very much.

Jul 19 '05 #1
4 12796
"fr**********@europe.com"
I'm trying to store a text within a MySQL field (v 3.23.58) by using
MySQLdb
(v 1.2.1c3).

The text is: "telephone..." (note the last character)

And I get this error message:
-----------
File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line
33, in defaulterrorhandler
raise errorclass, errorvalue
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2026' in
position 288: ordinal not in range(256)
-----------------------

Position 288 is the character I've mentioned. I suppose I must encode
this caracter
into a right one which MySQL could store, but I have no idea about how
to perform
it. Any suggestion?


the character \u2026 is not part of the ISO-8859-1 character set. if you
insist on storing that in 8-bit string, you have to find an 8-bit encoding
that includes that character (UTF-8 is one such alternative).

if MySQL is set to store ISO-8859-1 only, you can replace the character
with it with three periods, drop it (use the "ignore" encoding option) or
replace it with a suitable marker (use the "replace" encoding option).

</F>

Jul 19 '05 #2
Hi Fredrik.

Thank you very much for your quick answer.

Do you suggest to change it by using regexp or must I encode the whole
texto into a suitable one?

Regards.

Fredrik Lundh wrote:
"fr**********@europe.com"
I'm trying to store a text within a MySQL field (v 3.23.58) by using MySQLdb
(v 1.2.1c3).

The text is: "telephone..." (note the last character)

And I get this error message:
-----------
File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
raise errorclass, errorvalue
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2026' in position 288: ordinal not in range(256)
-----------------------

Position 288 is the character I've mentioned. I suppose I must encode this caracter
into a right one which MySQL could store, but I have no idea about how to perform
it. Any suggestion?
the character \u2026 is not part of the ISO-8859-1 character set. if

you insist on storing that in 8-bit string, you have to find an 8-bit encoding that includes that character (UTF-8 is one such alternative).

if MySQL is set to store ISO-8859-1 only, you can replace the character with it with three periods, drop it (use the "ignore" encoding option) or replace it with a suitable marker (use the "replace" encoding option).
</F>


Jul 19 '05 #3
fr**********@europe.com wrote:
File "/usr/lib/python2.3/site-packages/MySQLdb/connections.py", line 33, in defaulterrorhandler
raise errorclass, errorvalue
UnicodeEncodeError: 'latin-1' codec can't encode character u'\u2026' in position 288: ordinal not in range(256)
Thank you very much for your quick answer.

Do you suggest to change it by using regexp or must I encode the whole
texto into a suitable one?


a simple solution would be to manually create a table of problematic
unicode characters, use the translate method on the unicode string,
and then encode using the "replace" option.

charmap = {
0x2026: u"...",
# ...
}

text = u'telephone\u2026'

text = text.translate(charmap)
text = text.encode("iso-8859-1", "replace")

print text

http://docs.python.org/lib/string-methods.html

if you want more control of the replacement, you can skip the translate
step and use your own error handler, e.g.

charmap = ... see above ...

def fixunicode(info):
s = info.object[info.start:info.end]
try:
return charmap[ord(s)], info.end
except KeyError:
# fallback
return u"<U+%04x>" % ord(s), info.end

import codecs
codecs.register_error("fixunicode", fixunicode)

text = u'telephone\u2026'

text = text.encode("iso-8859-1", "fixunicode")

hope this helps!

</F>

Jul 19 '05 #4
Fredrik Lundh wrote:
[...]
if you want more control of the replacement, you can skip the translate
step and use your own error handler, e.g.

charmap = ... see above ...

def fixunicode(info):
s = info.object[info.start:info.end]
try:
return charmap[ord(s)], info.end


This will fail if there's more than one consecutive unencodable
character, better use
return charmap[ord(s[0])], info.start+1
or
return "".join(charmap.get(ord(c), u"<U+%04x>" % ord(c)) for c in
s), info.end
(without the try:) instead.

Bye,
Walter Dörwald
Jul 19 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: xyz | last post by:
I have a menu item to handle clipboard actions (cut, copy, paste). When I paste text that I copied from a Hungarian web page, the display looks normal on my RichTextBox, but the text retrieved from...
17
by: thinkfirst | last post by:
Hello CIWAH ... I want to propose full internationalization of three related websites: http://africadatabase.org/ http://people.africadatabase.org/ http://institutions.africadatabase.org/ My...
35
by: Dr.Tube | last post by:
Hi there, I have this web site (www.DrTube.com) which has the following DTD: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> which switches...
1
by: Markus Ernst | last post by:
Hi I wrote a function that "normalizes" strings for use in URLs in a UTF-8 encoded content administration application. After having removed the accents from latin characters I try to remove all...
8
by: Stanley Sinclair | last post by:
You wrote the pleural of "schema" as "schemata." My heart is warmed. Americans write that as "schemas," since they have no concept of the history of either their language nor their own...
9
by: Andy | last post by:
I am trying to write a for loop that will print all the ISO-Latin characters to a database. However: I am not sure exactly how to go about printing the ISO-Latin character set. Would anyone be...
2
by: Piotr | last post by:
Hi, I have set up, table export to mysql database and works weel bout I have problem with polish fonts ? Is tehere any way to change coding on the fly ? regards Peter
27
by: Pom | last post by:
Hello I want to convert a Mysql resulset to a dictionary. I made some code myself, and want to ask you if I do this the right way. def remapmysql(a): return (a, (a)) def test_map():
0
by: mistral | last post by:
What is best selection for 'collation' parameter - for those, who need use two languages, latin(english) and cyrillic(russian) on website and need that mySQL handled and stored both languages...
10
by: ahoway | last post by:
I am having problems entering a sentence for translating into pig latin. It is set up now to read the entire sentence as one word. I would like to know how to look at each word in the sentence so...
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:
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...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.