处理Python和MySQL中的特殊字符 [英] dealing with special characters in Python and MySQL

查看:406
本文介绍了处理Python和MySQL中的特殊字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为zingers的MySQL数据库。结构是:


zid - 整数,键,自动增量

关键字 - varchar

引文 - 文字

引用 - 文本


编码和校对是utf-8

字符和标点符号似乎都没有被存储和检索

正确。


来自Microsoft Word的特殊撇号和单引号导致了一个特殊的问题,即使我在尝试保存到数据库时还有所有的错误信息:

UnicodeDecodeError:''ascii''编解码器无法解码位置的字节0xe2

71:序数不在范围内(128)

args =(''ascii'',更新zingers set keywords =''aaaaaa; Action'',

.... ation ='''''''''在'zid = 422,71,72,''序数不是

的范围内('''''''''''' 128)'')

encoding =''ascii''

end = 72

object =" update zingers set keywords ='' aaaaaa;行动'',

.... ation =''''''''''' ''''''''''\ n \\ n'''其中zid = 422"

reason =''ordinal不在范围内(128)''

start = 71

我认为我的问题可能是我需要在

之前对字符串进行编码,将其保存在数据库中。有没有人能指出我正确的方向

在这里?



非常感谢,

解决方案

ronrsr写道:


我有一个名为zingers的MySQL数据库。结构是:


zid - 整数,键,自动增量

关键字 - varchar

引文 - 文字

报价 - 文本


编码和整理是utf-8

我在存储文本时遇到问题,如最后两个输入的那样领域。特殊的

字符和标点符号似乎都没有被存储和检索

正确。



你是在传递字符串作为Unicode字符串,还是作为其他东西?

如果你正在使用别的东西,有什么你告诉

数据库它是什么?


< / F>

DB的结构:


CREATE TABLE`zingers`(

`zid`int(9)unsigned NOT NULL auto_increment,

`contacts`varchar(255)默认为NULL,

`citation`文本,

`引用`文本,

PRIMARY KEY(`zid`)

)ENGINE = MyISAM DEFAULT CHARSET = UTF8 AUTO_INCREMENT = 422;


存储到数据库的代码:


querystring =" update zingers set keywords =''%s'',citation =

''%s'',quotation =%s''where zid =%d" %

(关键字,引用,引用,zid)

;


cursor.execute(查询字符串)


此时,querystring = update zingers set keywords =''

aaaaaa; Action'',citation ='''''''''''' ''''

'',quotation ='''''''''

''其中zid = 422


其中'''''''是各种各样的撇号和单引号复制

和从Word文档粘贴。


>

实际代码在哪里...你有什么提供没有显示

如何生成SQL ...

-


< blockquote>


>

你是在传递字符串作为Unicode字符串,还是作为别的东西?

如果你我正在使用别的东西,你做了什么来告诉

数据库是什么?



完全不确定我是什么传递它为。


数据库默认编码为utf-8
页面页面的编码是utf-8

我还要告诉数据库还有什么?


I have an MySQL database called zingers. The structure is:

zid - integer, key, autoincrement
keyword - varchar
citation - text
quotation - text

the encoding and collation is utf-8
I am having trouble storing text, as typed in last two fields. Special
characters and punctuation all seem not to be stored and retrieved
correctly.

Special apostrophes and single quotes from Microsoft Word are causing a
special problem, even though I have ''''ed all ''s

error messages, when trying to save to database:

UnicodeDecodeError: ''ascii'' codec can''t decode byte 0xe2 in position
71: ordinal not in range(128)
args = (''ascii'', "update zingers set keywords = '' aaaaaa;Action'',
....ation = ''''''''''''''''''''''''''\n\n '' where zid = 422", 71, 72, ''ordinal not
in range(128)'')
encoding = ''ascii''
end = 72
object = "update zingers set keywords = '' aaaaaa;Action'',
....ation = ''''''''''''''''''''''''''\n\n '' where zid = 422"
reason = ''ordinal not in range(128)''
start = 71
I think my problem may be that I need to encode the string before
saving it in the databse. Can anyone point me in the right direction
here?


Thanks so much,

解决方案

ronrsr wrote:

I have an MySQL database called zingers. The structure is:

zid - integer, key, autoincrement
keyword - varchar
citation - text
quotation - text

the encoding and collation is utf-8

I am having trouble storing text, as typed in last two fields. Special
characters and punctuation all seem not to be stored and retrieved
correctly.

are you passing in the strings as Unicode strings, or as something else?
if you''re using something else, what have you done to tell the
database what it is?

</F>


structure for the DB:

CREATE TABLE `zingers` (
`zid` int(9) unsigned NOT NULL auto_increment,
`keywords` varchar(255) default NULL,
`citation` text,
`quotation` text,
PRIMARY KEY (`zid`)
) ENGINE=MyISAM DEFAULT CHARSET=UTF8 AUTO_INCREMENT=422 ;


code for storing to database:

querystring = "update zingers set keywords = ''%s'', citation =
''%s'', quotation = %s'' where zid = %d" %
(keywords,citation,quotation,zid)
;

cursor.execute(querystring)

at this point, querystring = update zingers set keywords = ''
aaaaaa;Action'', citation = ''''''''''''''''''
'', quotation = ''''''''''''''''''''''''''

'' where zid = 422

where '''''''' are an assortment of apostrophes and single quotes copied
and pasted from a Word Document.

>
And where is the actual code... What you''ve supplied doesn''t show
how you are generating the SQL...
--


>
are you passing in the strings as Unicode strings, or as something else?
if you''re using something else, what have you done to tell the
database what it is?


not at all sure what I''m passing it as.

The database default encoding is utf-8
the database collation is utf-8
the page encoding of the page is utf-8

what else do I have to tell the database?


这篇关于处理Python和MySQL中的特殊字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆