从亿通行批量导出站名及翻译

有小伙伴问我站名是怎么导出的,索性把之前用来导出站名的一段小脚本分享出来。一方面提供方便,免得重复造轮子,另一方面也希望能起到抛砖引玉的作用。
挺菜的,没学过py,本行也不是写代码的。求大佬们轻喷。

北京地铁站名 新老英文译名对照表

运行环境:python3.7
所需依赖:httpx, openpyxl

# coding=utf-8

from datetime import *

from httpx import get as req
from openpyxl import Workbook
from openpyxl.styles import *

URL_RUUBY_PROD = "https://appconfig.ruubypay.com"

PATH_XLSX_FILE = "./英文译名对照表.xlsx"


res = req(URL_RUUBY_PROD+"/stations/map-h5.json")

if res.status_code != 200:
    print(str(res.status_code))
    exit()

raw = res.json()["stations_data"]

now = datetime.strptime(
    res.headers["last-modified"], '%a, %d %b %Y %H:%M:%S GMT')+timedelta(hours=8)


head = ["id", "中文名称", "英文名称", "双语译名1", "双语译名2", "纯英译名1", "纯英译名2", "对比结论"]
TAB = "\t"

wb = Workbook()
ws = wb.active

ws.column_dimensions["B"].width = 13
ws.column_dimensions["C"].width = 32
ws.column_dimensions["D"].width = 32
ws.column_dimensions["E"].width = 32
ws.column_dimensions["F"].width = 32
ws.column_dimensions["G"].width = 32
ws.column_dimensions["H"].width = 80


ws.title = "英文译名对照表"

ws["A1"] = "数据更新时间:"+now.strftime("%Y年%m月%d日 %H:%M:%S")+"\n"
ws["A2"] = "表格生成时间:"+datetime.now().strftime("%Y年%m月%d日 %H:%M:%S")+"\n\n"

for j in range(0, 8):
    ws.cell(row=4, column=j+1, value=head[j])

for i in range(0, len(raw)-1):

    row = i+5

    highlight = PatternFill("solid", fgColor="ff99cc")

    id = raw[i]["id"]

    cn = raw[i]["cn_name"]
    en = raw[i]["en_name"]

    eb1 = raw[i]["en_bilingual_station"][0]["name"]
    eb2 = ""
    if len(raw[i]["en_bilingual_station"]) == 1:
        pass
    elif len(raw[i]["en_bilingual_station"]) == 2:
        eb2 = raw[i]["en_bilingual_station"][1]["name"]
    elif len(raw[i]["en_bilingual_station"]) == 3:
        eb2 = raw[i]["en_bilingual_station"][1]["name"] + \
            " " + raw[i]["en_bilingual_station"][2]["name"]

    ee1 = raw[i]["en_station"][0]["name"]
    ee2 = ""
    if len(raw[i]["en_station"]) == 1:
        pass
    elif len(raw[i]["en_station"]) == 2:
        ee2 = raw[i]["en_station"][1]["name"]
    elif len(raw[i]["en_station"]) == 3:
        ee2 = raw[i]["en_station"][1]["name"] + \
            " " + raw[i]["en_station"][2]["name"]

    if eb2 != "":
        eb2 = eb2.lstrip()
        if eb2[0] in ["(", "("]:
            eb2 = eb2[1:]
            if eb2[-1] in [")", ")"]:
                eb2 = eb2[:-1]
            for c in eb2:
                if c in ["号", "临", "封", "停", "开", "改", "工"]:
                    eb2 = ""
                    break
        else:
            eb1 += " "+eb2
            eb2 = ""

    if ee2 != "":
        ee2 = ee2.lstrip()
        if ee2[0] in ["(", "("]:
            ee2 = ee2[1:]
            if ee2[-1] in [")", ")"]:
                ee2 = ee2[:-1]
            if "Open" in ee2 or "Clos" in ee2:
                ee2 = ""
        else:
            ee1 += " "+ee2
            ee2 = ""

    tmp = ""

    if eb2 != "":
        if eb2 != en:
            tmp += ", 英文名与双语译名2不同"
            ws.cell(row=row, column=5).fill = highlight
            ws.cell(row=row, column=3).fill = highlight
    else:
        if eb1 != en:
            tmp += ", 英文名与双语译名1不同"
            ws.cell(row=row, column=4).fill = highlight
            ws.cell(row=row, column=3).fill = highlight

    if ee2 != "":
        if ee2 != en:
            tmp += ", 英文名与纯英译名2不同"
            ws.cell(row=row, column=7).fill = highlight
            ws.cell(row=row, column=3).fill = highlight
    else:
        if ee1 != en:
            tmp += ", 英文名与纯英译名1不同"
            ws.cell(row=row, column=6).fill = highlight
            ws.cell(row=row, column=3).fill = highlight

    if eb1 != ee1:
        tmp += ", 双语译名1与纯英译名1不同"
        ws.cell(row=row, column=4).fill = highlight
        ws.cell(row=row, column=6).fill = highlight

    if eb2 != ee2:
        tmp += ", 双语译名2与纯英译名2不同"
        ws.cell(row=row, column=5).fill = highlight
        ws.cell(row=row, column=7).fill = highlight

    if tmp != "":
        tmp = tmp[2:]
        ws.cell(row=row, column=1).fill = PatternFill(
            "solid", fgColor="ff0000")

    value = [id, cn, en, eb1, eb2, ee1, ee2, tmp]

    for k in range(0, 8):
        ws.cell(row=row, column=k+1, value=value[k])


wb.save(PATH_XLSX_FILE)

运行结果(示意图):

1 个赞