Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Outputting Japanese Characters to a File using Python

Goal --> I am trying to automate the query execution process using Python

Detail --> My Source is Teradata Database and Destination is .txt file

I am writing a Python code to run a query in Teradata and save the output in .txt file.

Issue --> Even though,I am able to run the query and save the output,The Japanese Character are showing up as "\x1a\x1a"

For ex . when i run the query the output i see in Teradata SQL Assistant window is "愛してる”  while the output in text file is "\x1a\x1a"
I am using "PYCharm" for coding
I am using below code for writing the file

#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
import io  
import pyodbc  
import os  

#Establish connection with Teradata  
conn = pyodbc.connect('Coneection Parameters')  
conn.setencoding(encoding='utf-8')  
cur = conn.cursor()  
conn.setdecoding(pyodbc.SQL_CHAR, encoding='utf-8')

#Reading Query  
scriptFile = open('query.sql','r')  
script = scriptFile.read()  
scriptFile.close()  

#Executing Query  
cur.execute(script)  
rows = cur.fetchall()  

#Writing the output to file  
with open('results.txt','w') as f:  
   print(rows)  
   f.write('%s\n' % rows)  

#Closing the Connection  
cur.close()#close the query writing  
conn.close() 

The variables used
results.txt -> Target File,where i want to write the Japanese Character

Sample Output I am expecting ペット用品

The Output I am getting in File "results.txt" --> [('\x1a\x1a\x1a\x1a\x1a', )]

like image 985
Swapnil Avatar asked Feb 12 '26 12:02

Swapnil


1 Answers

Let's solve the title problem. To output Japanese (or any language) to a file:

  1. Start with a Unicode string.
  2. Open the file for writing and specify an encoding.
  3. Write the string to the file.

Example using Python 3:

s = 'ペット用品'
with open('results.txt','w',encoding='utf8') as f:
    f.write(s)

Your rows isn't a Unicode string, but a list with a tuple with an incorrect string. That's another problem you'll have to solve.

like image 172
Mark Tolonen Avatar answered Feb 16 '26 04:02

Mark Tolonen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!