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

Python多线程场景下如何拆分txt文件字段并将邮箱密码等存储到不同字符串

实现代码

直接修改原有代码即可达到需求:

import random
from concurrent.futures import ThreadPoolExecutor

FILENAME = 'testing/pokens.txt'
NTHREADS = 5

def myfunc():
    with open(FILENAME) as infile:
        lines = infile.readlines()
        line = random.choice(lines).strip()
        tokens = line.split(':')
        # 格式校验,避免行格式不符合要求时报错,不需要可以删除
        if len(tokens) < 3:
            return myfunc()
        # 直接返回拆分后的三元组,不提前拼接
        return (tokens[0], tokens[1], tokens[2])

def main():
    with ThreadPoolExecutor() as executor:
        futures = []
        # 用set存储三元组自动去重,三个字段完全一致才会判定为重复
        unique_records = set()
        for _ in range(NTHREADS):
            future = executor.submit(myfunc)
            futures.append(future)
        for future in futures:
            unique_records.add(future.result())
        
        # 分别提取三个字段拼接为逗号分隔的字符串
        email_str = ','.join([record[0] for record in unique_records])
        pass_str = ','.join([record[1] for record in unique_records])
        third_str = ','.join([record[2] for record in unique_records])

        # 以下为测试打印,可根据实际需求使用三个字符串变量
        print("邮箱集合字符串:", email_str)
        print("密码集合字符串:", pass_str)
        print("第三字段集合字符串:", third_str)

if __name__ == '__main__':
    main()

改动说明

  • 调整myfunc返回值,直接返回拆分后的字段元组,保留字段拆分状态方便后续分类提取
  • 保留原有去重逻辑,仅当整行三个字段完全重复时才会被过滤
  • 最终生成的email_str、pass_str、third_str三个变量就是你需要的独立存储的逗号分隔字符串,可以直接在后续逻辑中使用

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.24 19:24:04