用python将西里尔字母写入mysql

时间:2014-10-30 18:24:19

标签: python mysql encoding

我有脚本解析来自txt文件的数据(我认为它的utf8),然后它应该写入mysql。 当我做那样的事情时:

cur.execute("insert into keywords (keyword) values ('%s')" % (u'RUSSIAN TEXT HERE'))

它做得很好(我的mysqldb中有俄语字符串),但是当我

f = open('/Users/aaaa/Desktop/keywords.txt', 'rw+')
for x in range (14028):
    a = f.readline(x)
    cur.execute("insert into keywords (keyword) values ('%s')" % (a))
    #or that
    cur.execute("insert into keywords (keyword) values ('%s')" % (unicode(a)))

然后我得到了这个

'ascii' codec can't decode byte 0xd0 in position 0: ordinal not in range(128)

我的mysql中有这个

character_set_client    utf8
character_set_connection    utf8
character_set_database  utf8
character_set_filesystem    binary
character_set_results   utf8
character_set_server    latin1
character_set_system    utf8
character_sets_dir  /usr/local/mysql-5.6.20-osx10.8-x86_64/share/charsets/

偶尔,我会将simbols写入数据库,但俄语符号就像是正方形和东西 我该怎么办?

另外,我在repr(a)之后得到了这个

''
'0'
'5 '
'ru '
'\xd0\xbc\xd0\xb0'
'\xd1\x85\xd0\xb0\xd1'
Traceback (most recent call last):

1 个答案:

答案 0 :(得分:1)

尝试以这种方式打开文件:

f = open('/Users/aaaa/Desktop/keywords.txt', 'rw+', encoding='utf-8')