错误(1366,“错误的字符串值:'\\ xE9 des ...''列'标题'在第29行”)在导入csv中发生到mysql

时间:2018-05-27 12:19:53

标签: mysql csv

当我尝试将数据从csv文件转换为mysql时,发生了一个OperationalError:

OperationalError: (1366, "Incorrect string value: '\\xE9 des ...' for column 'title' at row 29")

我的sql配置如下(版本5.7.22):

mysql> show variables like '%char%';
+--------------------------------------+----------------------------+
| Variable_name                        | Value                      |
+--------------------------------------+----------------------------+
| character_set_client                 | utf8                       |
| character_set_connection             | utf8                       |
| character_set_database               | utf8                       |
| character_set_filesystem             | binary                     |
| character_set_results                | utf8                       |
| character_set_server                 | utf8                       |
| character_set_system                 | utf8                       |
| character_sets_dir                   | /usr/share/mysql/charsets/ |
| validate_password_special_char_count | 1                          |
+--------------------------------------+----------------------------+
9 rows in set (0.00 sec)

我的架构是

"""                                                                                    
...: CREATE TABLE `Movies` (
...: `id` int(11) not null auto_increment,
...: `title` varchar(255) default '',
...: `genres` varchar(255) default '',
...: primary key(`id`)) engine=innodb charset=utf8mb4"""

csv文件是从ml-latest-small.zip 下载的movies.csv。 要读取csv文件,需要编写一个函数:

def import_data(tablename, filename):
    with open(filename) as f:
        reader = csv.reader(f)
        keys = next(reader)
        keys[0] = 'id'   # change `movieId` in csv to `id`.
        database.insertmany(tablename, keys, reader)

当我运行import_data('Movies','movies.csv')时,发生了错误:

OperationalError: (1366, "Incorrect string value: '\\xE9 des ...' for column 'title' at row 29")

我检查了movies.csv中的第29行:

movieId,title,genres
30,Shanghai Triad (Yao a yao yao dao waipo qiao) (1995),Crime|Drama

我无法弄清楚原因。有没有任何想法的人?非常感谢〜

0 个答案:

没有答案