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.0.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.0-py3-none-any.whl (9.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: teradata_latin-0.1.0.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.0.tar.gz
Algorithm Hash digest
SHA256 02e913f02bab19ac5e699b97c3538d3075d70eb8fe235628d3e41dd3a9ee1e31
MD5 446ef6c38bdbfa7dff72824d0118b330
BLAKE2b-256 c64af157ed25bcff73d796fb4474be9aee7898d220cf3cc62b27857dc3b22bf7

See more details on using hashes here.

Provenance

The following attestation bundles were made for teradata_latin-0.1.0.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.0-py3-none-any.whl.

File metadata

  • Download URL: teradata_latin-0.1.0-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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c1f25557179cbc43920389e4ce298cb90f73bb8241af6166c7f2b8ef879aaaf3
MD5 875125ca09751968f5796e1720f78a91
BLAKE2b-256 2acfa1ee161c8ec5394b31ff682a4f6583e19d04c9cbcfdfd9a5de9a3831be96

See more details on using hashes here.

Provenance

The following attestation bundles were made for teradata_latin-0.1.0-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