问题
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', )]
回答1:
Let's solve the title problem. To output Japanese (or any language) to a file:
- Start with a Unicode string.
- Open the file for writing and specify an encoding.
- 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.
来源:https://stackoverflow.com/questions/47897384/outputting-japanese-characters-to-a-file-using-python