开发者

In which encoding does the shell command [mysql -e "..." -u ... > some_file.sql] store the content into some_file.sql?

I wanted to export the results of a certain query into a file and therefore issued the shell command

mysql -e "SELECT some, thing FROM some_where" -u my_user -p my_database > some_file.sql

I should mention, that the table and the columns are stored in utf8. When trying to iterate through the开发者_如何学编程 lines by this python code

with codecs.open(PATH_TO_SQL_FILE, 'r', 'utf8') as read_file:
    for line in read_file:
        print line

I get an error:

UnicodeDecodeError: 'utf8' codec can't decode byte 0xf3 in position 1: invalid continuation byte

If I remove the usage of the codecs, the script finishes without errors but I get to see in the terminal things like:

Zolt�n L�szl�

What do I miss? Should I use another codec? How can I determine which one codec to use for decoding? All hints are appreciated!

By the way, if I open the file some_file.sql with vi in terminal every diacritic character is shown the right way.


mysql uses Latin-1 as the default character set unless you tell it otherwise. You can either decode from iso-8859-1 in python, or use:

mysql --default-character-set=utf-8 -e "SELECT some, thing FROM some_where" -u my_user -p my_database > some_file.sql
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜