将utf-8编码的文本加载到MySQL表中

发布于 2024-10-17 04:54:41 字数 217 浏览 3 评论 0原文

我有一个大的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

独﹏钓一江月 2024-10-24 04:54:41

尝试

LOAD DATA INFILE 'file'
IGNORE INTO TABLE table
CHARACTER SET UTF8
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'

Try

LOAD DATA INFILE 'file'
IGNORE INTO TABLE table
CHARACTER SET UTF8
FIELDS TERMINATED BY ';'
OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
深空失忆 2024-10-24 04:54:41

正如 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

黑色毁心梦 2024-10-24 04:54:41

不需要对文件中的字符进行编码,但在将此文件加载到数据库之前,您需要确保文件采用 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.

心碎的声音 2024-10-24 04:54:41

你应该

init_command = 'SET NAMES UTF8'
use_unicode = True
charset = 'utf8'

在执行 MySQLdb.connect() 时 发送
例如

dbconfig = {}
dbconfig['host']            = 'localhost'
dbconfig['user']            = ''
dbconfig['passwd']          = ''
dbconfig['db']              = ''
dbconfig['init_command']    = 'SET NAMES UTF8'
dbconfig['use_unicode']     = True
dbconfig['charset']         = 'utf8'

conn = MySQLdb.connect(**dbconfig)

编辑:啊,抱歉,我看到您已经添加了您正在使用“LOAD DATA LOCAL INFILE”——这从您最初的问题中并不清楚:)

You should send

init_command = 'SET NAMES UTF8'
use_unicode = True
charset = 'utf8'

when doing MySQLdb.connect()
e.g.

dbconfig = {}
dbconfig['host']            = 'localhost'
dbconfig['user']            = ''
dbconfig['passwd']          = ''
dbconfig['db']              = ''
dbconfig['init_command']    = 'SET NAMES UTF8'
dbconfig['use_unicode']     = True
dbconfig['charset']         = 'utf8'

conn = MySQLdb.connect(**dbconfig)

edit: ah, sorry, I see you've added that you're using "LOAD DATA LOCAL INFILE" -- this wasn't clear from your initial question :)

丢了幸福的猪 2024-10-24 04:54:41

尝试类似

加载数据本地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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文