我在python中做一些脚本.我创建了一个保存在文件中的字符串.这个字符串有很多数据,来自目录的树状和文件名.根据convmv,我所有的树状花序都是UTF-8.
我想把所有内容都保存在UTF-8中,因为我之后会把它保存在MySQL中.现在,在UTF-8的MySQL中,我遇到了一些问题(比如é或è - 我是法国人).
我希望python总是使用字符串作为UTF-8.我在互联网上阅读了一些信息,我确实喜欢这个.
我的脚本以此开头:
#!/usr/bin/python
# -*- coding: utf-8 -*-
def createIndex():
import codecs
toUtf8=codecs.getencoder('UTF8')
#lot of operations & building indexSTR the string who matter
findex=open('config/index/music_vibration_'+date+'.index','a')
findex.write(codecs.BOM_UTF8)
findex.write(toUtf8(indexSTR)) #this bugs!
Run Code Online (Sandbox Code Playgroud)
当我执行时,这里是答案: UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 2171: ordinal not in range(128)
编辑:我看到,在我的文件中,重音很好写.创建此文件后,我将其读取并将其写入MySQL.但我不明白为什么,但我遇到编码问题.我的MySQL数据库是在utf8中,或者似乎是SQL查询SHOW variables LIKE 'char%'只返回utf8或二进制.
我的功能看起来像这样:
#!/usr/bin/python
# -*- coding: utf-8 -*-
def saveIndex(index,date):
import MySQLdb as mdb
import codecs
sql = mdb.connect('localhost','admin','*******','music_vibration')
sql.charset="utf8"
findex=open('config/index/'+index,'r')
lines=findex.readlines()
for line in lines:
if line.find('#artiste') …Run Code Online (Sandbox Code Playgroud) 我有以下python脚本(tes.py):
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import MySQLdb
query = "INSERT INTO test(test) VALUES ('ñ')"
print query + "\n"
conn = MySQLdb.connect (host = "localhost", user = "ibrick", passwd = "x", db = "ibrick", charset="utf8")
conn.names="utf8"
cursor = conn.cursor()
cursor.execute (query);
cursor.close ()
conn.commit ()
Run Code Online (Sandbox Code Playgroud)
文件编码utf-8:
$ file -i tes.py
tes.py: text/x-java charset=utf-8
Run Code Online (Sandbox Code Playgroud)
系统编码UTF:
#locale
LANG=es_AR.UTF-8
LC_CTYPE="es_AR.UTF-8"
LC_NUMERIC="es_AR.UTF-8"
LC_TIME="es_AR.UTF-8"
LC_COLLATE="es_AR.UTF-8"
LC_MONETARY="es_AR.UTF-8"
LC_MESSAGES="es_AR.UTF-8"
LC_PAPER="es_AR.UTF-8"
LC_NAME="es_AR.UTF-8"
LC_ADDRESS="es_AR.UTF-8"
LC_TELEPHONE="es_AR.UTF-8"
LC_MEASUREMENT="es_AR.UTF-8"
LC_IDENTIFICATION="es_AR.UTF-8"
LC_ALL=
echo "ñññ" > /tmp/test.txt
file /tmp/test.txt
/tmp/test.txt: UTF-8 Unicode …Run Code Online (Sandbox Code Playgroud) 我已经想出了这个问题,但经过一些测试后我决定用一些更具体的信息创建一个新问题:
我正从Active Directory中使用python-ldap(和Python 2.7)读取用户帐户.这确实很好用,但我有特殊字符的问题.在控制台上打印时,它们看起来像UTF-8编码的字符串.目标是将它们写入MySQL数据库,但我从一开始就没有将这些字符串写入正确的UTF-8.
示例(fullentries是包含所有AD条目的数组):
fullentries[23][1].decode('utf-8', 'ignore')
print fullentries[23][1].encode('utf-8', 'ignore')
print fullentries[23][1].encode('latin1', 'ignore')
print repr(fullentries[23][1])
Run Code Online (Sandbox Code Playgroud)
用手插入字符串的第二次测试如下:
testentry = "M\xc3\xbcller"
testentry.decode('utf-8', 'ignore')
print testentry.encode('utf-8', 'ignore')
print testentry.encode('latin1', 'ignore')
print repr(testentry)
Run Code Online (Sandbox Code Playgroud)
第一个例子的输出是:
M\xc3\xbcller
M\xc3\xbcller
u'M\\xc3\\xbcller'
Run Code Online (Sandbox Code Playgroud)
编辑:如果我尝试用.replace('\\\\','\\)替换双反斜杠,则输出保持不变.
第二个例子的输出:
Müller
M?ller
'M\xc3\xbcller'
Run Code Online (Sandbox Code Playgroud)
有没有办法让AD输出正确编码?我已经阅读了很多文档,但它们都声明LDAPv3为您提供严格的UTF-8编码字符串.Active Directory使用LDAPv3.
我的老问题这个主题在这里:使用Python将UTF-8字符串写入MySQL
编辑:添加了repr(s)信息
我试图使用MySQLdb驱动程序将一些阿拉伯语单词插入arabic_word我的hanswehr2数据库Maria DB 的列中.
我得到了一个latin-1 encode error.但在阅读之后,我发现MySQLdb驱动程序是默认的latin-1,我必须在函数中明确设置utf-8为我的选择字符集mariadb.connect().酱.
整个数据库设置为utf-8.
码:
def insert_into_db(arabic_word, definition):
try:
conn = mariadb.connect('localhost', 'root', 'xyz1234passwd', 'hans_wehr', charset='utf-8', use_unicode=True)
conn.autocommit(True)
cur = conn.cursor()
cur.execute("INSERT INTO hanswehr2 (arabic_word , definition) VALUES (%s,%s)", (arabic_word, definition,))
except mariadb.Error, e:
print e
sys.exit(1)
Run Code Online (Sandbox Code Playgroud)
但是现在我收到以下错误:
/usr/bin/python2.7 /home/heisenberg/hans_wehr/main.py
Total lines 87672
(2019, "Can't initialize character set utf-8 (path: /usr/share/mysql/charsets/)")
Process finished with exit code 1
Run Code Online (Sandbox Code Playgroud)
我已经指定Python MySQL驱动程序使用utf-8字符,但它似乎忽略了这一点.
任何投入都将受到高度赞赏.