将utf-8编码的文本加载到MySQL表中
我有一个大的 CSV 文件,我要将其加载到 MySQL 表中。然而,这些数据被编码为utf-8格式,因为它们包含一些非英语字符。 我已经将表中相应列的字符集设置为utf-8。但是当我加载我的文件时。非英语字符变成奇怪的字符(当我在表行上进行选择时)。在将数据加载到表中之前是否需要对数据进行编码?如果是的话我该怎么做。我正在使用 Python 加载数据并使用 LOAD DATA LOCAL INFILE 命令。 谢谢
I have a large CSV file that I am going to load it into a MySQL table. However, these data are encoded into utf-8 format, because they include some non-english characters.
I have already set the character set of the corresponding column in the table to utf-8. But when I load my file. the non-english characters turn into weird characters(when I do a select on my table rows). Do I need to encode my data before I load the into the table? if yes how Can I do this. I am using Python to load the data and using LOAD DATA LOCAL INFILE command.
thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试
Try
正如 http://dev.mysql.com/doc/ 中所述refman/5.1/en/load-data.html,
您可以使用 LOAD DATA LOCAL INFILE 的“CHARACTER SET”可选参数指定 CSV 文件使用的字符集
as said in http://dev.mysql.com/doc/refman/5.1/en/load-data.html,
you can specify the charset used by your CSV file with the "CHARACTER SET" optional parameter of LOAD DATA LOCAL INFILE
不需要对文件中的字符进行编码,但在将此文件加载到数据库之前,您需要确保文件采用 UTF-8 编码。
Do not need encode your characters in the file, but you need to make sure that your file is encoding at UTF-8 before load this file to database.
你应该
在执行 MySQLdb.connect() 时 发送
例如
编辑:啊,抱歉,我看到您已经添加了您正在使用“LOAD DATA LOCAL INFILE”——这从您最初的问题中并不清楚:)
You should send
when doing MySQLdb.connect()
e.g.
edit: ah, sorry, I see you've added that you're using "LOAD DATA LOCAL INFILE" -- this wasn't clear from your initial question :)
尝试类似
加载数据本地INFILE“文件”
进入表 message_history
字符集UTF8
以“|”结尾的列
可选择用“”括起来
ESCAPED BY '"';
原始结构,
https ://dev.mysql.com/doc/refman/8.0/en/load-data.html
Try something like,
LOAD DATA LOCAL INFILE "file"
INTO TABLE message_history
CHARACTER SET UTF8
COLUMNS TERMINATED BY '|'
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"';
Original Structure,
https://dev.mysql.com/doc/refman/8.0/en/load-data.html