Loading utf-8 encoded text into MySQL table

后端 未结 5 729
小蘑菇
小蘑菇 2020-11-29 06:37

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

5条回答
  •  再見小時候
    2020-11-29 07:07

    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 :)

提交回复
热议问题