Skip to main content

teradata-latin

Teradata LATIN 字符集中文编解码组件(纯标准库,零依赖)。

问题背景

Teradata 的 LATIN 字符集列中常见以 GBK 编码存储的中文(历史 ETL 通过 CLIENT_CHARSET=GBK 的 JDBC 会话直通写入)。使用官方纯 Python 驱动 teradatasql(固定 UTF-8 会话、 不支持 charset 参数)读取时,服务端按 Teradata LATIN 代码页逐字节映射 Unicode,落在 0x80-0x9F / 0xD0-0xFE 等区间的 GBK 字节会被映射为不同 码位(如 0xD0 -> U+00F00xDE -> U+00FE0xD7 -> U+01520xF7 -> U+01530xB8 -> U+0160),中文变成乱码。

tdlatin 提供与生产 JDBC(CHARSET=GBK)等价的转换:

  • 读取方向:乱码字符 -> 原始字节(Teradata LATIN 映射)-> GBK 解码 = 正确中文
  • 写入/过滤方向:正确中文 -> GBK 字节 -> Teradata LATIN 映射字符, 嵌入 SQL 字面量后由服务端 UNICODE_TO_LATIN 转回原始字节,比较语义与生产一致

安装

pip install teradata-latin

用法

import teradatasql
from teradata_latin import decode_td_latin, encode_td_latin

con = teradatasql.connect(host='...', user='...', password='...')

# 1) 读出中文列 -> 还原
cur = con.cursor()
cur.execute("SELECT TOP 5 Client_CN_Name FROM pv_pmart.DIM_CLIENT_BASE_INFO")
for (name,) in cur.fetchall():
    print(decode_td_latin(name))   # '某国际货运代理有限公司'

# 2) SQL 中文字面量 / 中文参数 -> 编码后嵌入
lit = encode_td_latin('0未逾期')
cur.execute("SELECT count(1) FROM t WHERE AR_Overdue_Mth IN ('%s','')" % lit)

name = encode_td_latin('某工业环保设备有限公司')
cur.execute("SELECT Client_id FROM t WHERE Client_CN_Name = '%s'" % name)

API

函数 说明
decode_td_latin(text, errors='replace') 读出乱码 -> 正确中文;无法映射时安全原样返回(或 errors='strict' 抛错)
encode_td_latin(text, errors='strict') 中文 -> SQL 字面量;GBK 不可编码字符可 errors='replace'
TDLatinCoder 面向对象封装(decode / encode

映射表可通过 TDBYTE_TO_UNICODE / UNICODE_TO_TDBYTE / DRIFT_BYTE_COUNT 访问(196 个实测字节,18 个与标准 Latin-1 不一致)。

映射表来源

多张线上业务表(订单/客户/财务/营收/逾期)共 9000+ 行采样 + Teradata UNICODE_TO_LATIN / LATIN_TO_UNICODE 服务端往返实验验证,确定性一一映射, 无冲突;漂移字节全部通过服务端往返校验。

License

MIT

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

teradata_latin-0.1.1.tar.gz (10.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

teradata_latin-0.1.1-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

Details for the file teradata_latin-0.1.1.tar.gz.

File metadata

  • Download URL: teradata_latin-0.1.1.tar.gz
  • Upload date:
  • Size: 10.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for teradata_latin-0.1.1.tar.gz
Algorithm Hash digest
SHA256 80ae5b1aca4544ca38b5e714115b1ec0d77bd1ace4597b848a67d68d7ea267e1
MD5 ca278a26aa4c6e52e7ac344b9b77d568
BLAKE2b-256 71d6ace0b52288dbf9ff0113ed5514654329ef271d13b909bfac39eaec1615cb

See more details on using hashes here.

Provenance

The following attestation bundles were made for teradata_latin-0.1.1.tar.gz:

Publisher: publish.yml on yujchn/teradata-latin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file teradata_latin-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: teradata_latin-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 9.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for teradata_latin-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 fc4021ecf6cb321c68b808a05d05a3bbb31c434ae48f4a51e876bbd10a0a30be
MD5 6a9901dceadbbc4dff7ff7d005a1f555
BLAKE2b-256 7814a107311321964a8d47989d333e95380c9cecdbbdc6755f21f8a29dc73d02

See more details on using hashes here.

Provenance

The following attestation bundles were made for teradata_latin-0.1.1-py3-none-any.whl:

Publisher: publish.yml on yujchn/teradata-latin

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page