I have data in the database and I want to export it to CSV file. The data is in Japanese and for further use, I need it to be encoded in UTF-8
This is my script to get the data and write in the CSV file
import mysql.connector
from mysql.connector import errorcode
import sys
import csv
query = 'SELECT * FROM `images-data`'
try:
cnx = mysql.connector.connect(user='root', password='1234',
host='127.0.0.1',
database='sotsuken-test-db')
cur=cnx.cursor()
cur.execute(query)
result=cur.fetchall()
c = csv.writer(open("db-data.csv","w"))
for row in result:
c.writerow(row)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
else:
cnx.close()
I can create the CSV file, and the data is exported in UTF-8, but my CSV file's data was like this:
1,b'\xe3\x83\x86\xe3\x82\xb9\xe3\x83\x88\xe3\x81\xa7\xe3\x81\x94\xe3\x81\x8a\xe3\x81\x96\xe3\x81\x84\xe3\x81\xbe\xe3\x81\x99'
In my research I found that my data was written in byte object (may be I'm wrong here). I need the data to be written in UTF-8 without b''
I know that I can use decode("UTF-8") to make it into the string, but I cannot apply that to csv.writerow
Can you give me any suggestions?
DictWritersolve this any better thanwriter? Sure, it's nicer for other reasons, if you know what your columns are, but it doesn't change the fact that some of those columns arebytes.fetchallhere. You can just iteratefor row in cur:and the cursor should give you one row at a time, buffering up as many rows in memory as it thinks would be most efficient, instead of reading them all into memory no matter what. Also, it's usually a bad idea to not close files you opened for writing, either with aclose()call or awithstatement.