2

I'm very new to Python and MySQL, I have dumped a file in database using python, on executing a query Select * from table_name I am getting the result in Unicode format i.e

(1, u'John',u'smith')
(2, u'Markus',u'Doe')
(3, u'Andrew',u'smith')

but, I required in this format

(1, 'John','smith')
(2, 'Markus','Doe')
(3, 'Andrew','smith')

The file using encoding="utf-8" format Because of this unicode format, I'm unable to perform any insertion based on foreign keys

python code for this problem:

with codecs.open("input.txt", "r", encoding="utf-8") as f:
   for num,line in enumerate(f,1):
        try:

            words=line.split('\t')
            list =[]
            for word in words:
                list.append(word.encode("utf-8"))

            args = (list[0],list[0])
            cursor.execute(query,args)
f.close()
cursor.execute("Select * from table_name")
rows = cursor.fetchall()

print('Total Row(s):', cursor.rowcount)
for row in rows:
     print [type(word) for word in row]



conn.commit()         
conn.close()

Please help me, thank you in advance.

4
  • possible duplicate of : stackoverflow.com/questions/9154998/python-encoding-mysql Commented Jul 4, 2017 at 10:26
  • we have already tried the above link but not solving my problem Commented Jul 4, 2017 at 10:32
  • I don't know your python code. Possible to share it ? Meanwhile look at this : stackoverflow.com/questions/25121829/… Commented Jul 4, 2017 at 10:36
  • I have uploaed the code and Meanwhile on inserting data to another table, I'm facing mismatch on foreign keys Commented Jul 4, 2017 at 10:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.