import csv into MySQL ( UnicodeDecodeError: 'charmap' codec can't decode byte )

 0

I have a problem when importing csv.

my code:

import MySQLdb
import csv
from datetime import datetime
conn = MySQLdb.connect(host="192.168.2.20", user="xxx", password="xxxx", database="admin_mt")
cursor = conn.cursor()
cursor.execute("TRUNCATE TABLE wp_dbo_iclock_transaction2")

csv_data = csv.reader(open('dbo_iclock_transaction.csv'))
header = next(csv_data)


print('Importing the CSV Files')
for row in csv_data:
  
    punch_time = datetime.strptime(row[2], '%d/%m/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S')
    
    cursor.execute("""INSERT INTO wp_dbo_iclock_transaction2
                                  (id, emp_code, punch_time)
                                       VALUES (%s,%s,%s)""",
                                       (row[0], row[1], punch_time))

conn.commit()
cursor.close()
print('Done')

The error I get :

Traceback (most recent call last):
  File "D:\Server Backup\time_import\import.py", line 13, in <module>
    header = next(csv_data)
  File "C:\Program Files\Python311\Lib\encodings\cp874.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x8a in position 310: character maps to <undefined>

I'm not sure what caused the problem. Or maybe it's because the csv file is encoding utf8 !


The error message you are seeing suggests that there is a problem with the character encoding of the CSV file. The default encoding used by the csv module is the system's default encoding, which might not be able to decode the characters in your CSV file.

You can try specifying the encoding explicitly when opening the CSV file, like this:

kotlin
csv_data = csv.reader(open('dbo_iclock_transaction.csv', encoding='utf-8'))

This assumes that your CSV file is encoded in UTF-8. If it's encoded in a different encoding, you'll need to specify that instead.

Additionally, you may want to check if the data in the CSV file contains any non-ASCII characters that could be causing the error. You can open the CSV file in a text editor and look for any characters that look strange or out of place, or you can try using a tool like chardet to automatically detect the file's encoding.

If the above suggestions do not solve the issue, you may need to investigate the content of the CSV file further to determine what is causing the error.

Comments