You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为NumPy数组中每个ID前后加字符生成赛马站点URL并导出文本

实现方案

直接基于原始NumPy数组生成(推荐)

无需先生成纯ID的文本文件,可直接在原有代码基础上修改,批量拼接前后缀后写入即可:

import numpy as np

a = np.array([2019104896, 2016102929, 2018105085]) 
prefix = "https://db.netkeiba.com/horse/"
suffix = "/"

# 转为字符串后批量拼接前后缀
b = a.astype(str)
url_arr = np.char.add(np.char.add(prefix, b), suffix)

# 按行写入文件
with open('horse_url.txt', 'w') as f:
    f.write("\n".join(url_arr))

运行后生成的horse_url.txt文件每行即为完整的目标URL。

基于已生成的纯ID文件转换

如果你已经生成了仅存ID的horse_id.txt,也可以直接读取该文件做二次处理:

prefix = "https://db.netkeiba.com/horse/"
suffix = "/"

with open('horse_id.txt', 'r', encoding='utf-8') as f_in, open('horse_url.txt', 'w', encoding='utf-8') as f_out:
    for line in f_in:
        horse_id = line.strip()
        if not horse_id:
            continue
        f_out.write(f"{prefix}{horse_id}{suffix}\n")

内容的提问来源于stack exchange,提问作者zyg

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 07:18:02