我希望将某个查询的结果导出到一个文件中,因此发出了shell命令
mysql -e "SELECT some, thing FROM some_where" -u my_user -p my_database > some_file.sql值得一提的是,表和列存储在utf8中。当尝试通过以下python代码遍历各行时
with codecs.open(PATH_TO_SQL_FILE, 'r', 'utf8') as read_file:
for line in read_file:
print line我得到一个错误:
UnicodeDecodeError: 'utf8' codec can't decode byte 0xf3 in position 1: invalid continuation byte如果我删除了codecs的用法,脚本结束时没有出现错误,但我在终端中看到了如下内容:
Zolt�n L�szl�我错过了什么?我应该使用另一个编解码器吗?如何确定使用哪个编解码器进行解码?感谢所有的提示!
顺便说一下,如果我在终端中用vi打开文件some_file.sql,每个变音符号都会以正确的方式显示。
发布于 2011-08-05 12:17:43
除非您另行说明,否则mysql将使用拉丁语-1作为默认字符集。您可以在python中对iso-8859-1进行解码,或者使用:
mysql --default-character-set=utf-8 -e "SELECT some, thing FROM some_where" -u my_user -p my_database > some_file.sqlhttps://stackoverflow.com/questions/6951332
复制相似问题