26 lines
795 B
Python
26 lines
795 B
Python
"""A 股代码归一化。支持 6 位纯数字或带交易所后缀(000001 / 000001.SZ)。"""
|
||
from __future__ import annotations
|
||
|
||
|
||
def plain_code(code: str) -> str:
|
||
"""000001.SZ -> 000001"""
|
||
return code.strip().upper().split(".")[0]
|
||
|
||
|
||
def to_ts_code(code: str) -> str:
|
||
"""转 Tushare ts_code(带交易所后缀)。"""
|
||
c = code.strip().upper()
|
||
if "." in c:
|
||
return c
|
||
c = plain_code(c)
|
||
# 沪市:60xxxx 主板、68xxxx 科创、9xxxxx B 股
|
||
if c.startswith(("60", "68", "9")):
|
||
return c + ".SH"
|
||
# 深市:00xxxx 主板/中小、30xxxx 创业、20xxxx B 股
|
||
if c.startswith(("00", "30", "20")):
|
||
return c + ".SZ"
|
||
# 北交所:8xxxxx / 4xxxxx
|
||
if c.startswith(("8", "4")):
|
||
return c + ".BJ"
|
||
return c + ".SZ"
|