【问题标题】:Writing with Python Greek letters to (Oracle) SQL file doesn't work用 Python 希腊字母写入(Oracle)SQL 文件不起作用
【发布时间】:2020-11-02 23:10:25
【问题描述】:

我正在尝试将希腊字母写入扩展名为“.sql”的文件。但是文件中的结果不正确。

Python中的代码:

with open('test.sql', 'w', encoding='utf-8') as test_file:
    test_file.write('DH5α\n')

在我得到的 SQL 文件中:

DH5α

有什么办法可以解决这个问题?

操作系统是windows

谢谢。

【问题讨论】:

  • 你是如何打开文件的?可能您使用的任何程序都没有使用正确的编码,在这种情况下,utf-8
  • 正确,文件在sql-developer中打开。我不得不在 sql-developer 中将编码更改为 UTF-8。我以为 python 函数中的 [encoding='utf-8'] 已经在处理这个了,但我猜不是。
  • 处理文件包含的内容。不是其他程序如何解释它。如果您在 Windows 上,可能会采用“拉丁”编码,即 ISO-8859-1

标签: python sql python-3.x oracle oracle-sqldeveloper


【解决方案1】:

从 cx_Oracle 文档 Character Sets and Globalization 确保您的连接使用正确的字符集打开。

import cx_Oracle
connection = cx_Oracle.connect(connectString, encoding="US-ASCII", nencoding="UTF-8")

请注意,在 cx_Oracle 8 中,默认值更改为“UTF-8”。

【讨论】:

    【解决方案2】:

    因为用 cmets 写的,问题出在 sql-developer 本身,它默认不使用 utf-8 编码。

    【讨论】:

      猜你喜欢
      • 2021-07-27
      • 2014-02-01
      • 1970-01-01
      • 2016-11-05
      • 1970-01-01
      • 2019-11-07
      • 1970-01-01
      • 1970-01-01
      • 2020-05-22
      相关资源
      最近更新 更多