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

Python密码增强脚本开发:首字母大写与追加年份后缀问题求助

可行解决代码
with open("passwordlist.txt", "r", encoding="utf-8") as reading_file:
    # 预生成1900-2014所有年份的字符串列表
    year_list = [str(year) for year in range(1900, 2015)]
    with open("hashedpasslist.txt", "w", encoding="utf-8") as writing_file:
        for line in reading_file:
            raw_pwd = line.strip()
            if not raw_pwd:
                continue
            # 按需求顺序执行转换
            reversed_pwd = raw_pwd[::-1] # 反转字符串
            replaced_pwd = reversed_pwd.replace("o", "0").replace("a", "4").replace("s", "5") # 字符替换
            base_pwd = replaced_pwd.capitalize() # 首字符大写
            # 批量追加年份后缀写入文件
            for year in year_list:
                writing_file.write(f"{base_pwd}{year}\n")
核心问题解决说明
  • 首字符大写:直接调用Python字符串内置的capitalize()方法即可实现,无需手动替换指定字符,该方法会自动将字符串首字符转为大写,其余字符转为小写,完全匹配需求。
  • 批量追加年份后缀:先生成1900到2014的所有年份字符串列表(注意range为左闭右开规则,要包含2014需要将结束参数设为2015),遍历年份列表和处理后的基础密码拼接,逐行写入结果文件即可。
  • 额外优化:使用with上下文管理器操作文件,无需手动调用close()方法关闭文件,避免资源泄漏问题;新增空行跳过逻辑,兼容原密码文件存在空行的情况。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 23:45:00