吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1176|回复: 16
收起左侧

[资源求助] 谁会python 文本txt去重了

[复制链接]
wangsheng518 发表于 2025-6-7 22:10
100吾爱币
请问小伙伴们,谁会python 文本txt去重了  之前同学做的是C语言,每次都是改成ANSI编码
然后复制 文本的属性, 安全里面的对象名称的路径操作 。




然后点击去重 ,这个操作每次要用好几秒 默认去重后样式已经在
https://wwij.lanzout.com/iBEuz2yav0oh    去重前 去重后的文本 在里面 需要的就是 txt 去重后的样式。因为之前同学做的C语言去重,每次打开报错病毒,需要关闭 为“病毒和威胁防护“设置  实施保护 全部关闭 谢谢






   
        
        
        
   

最佳答案

查看完整内容

在楼上大佬代码的基础上优化了下,去重判断以题干文字 [mw_shl_code=python,true]import re import os import tkinter as tk from tkinter import filedialog # 获取程序所在目录 script_dir = os.path.dirname(os.path.abspath(__file__)) # 文件路径定义(默认设置为程序所在目录) root = tk.Tk() root.withdraw() # 隐藏主窗口 # 选择输入文件(默认打开程序所在目录) input_file = filedialog.askopenfile ...

发帖前要善用【论坛搜索】功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

cioceo 发表于 2025-6-7 22:10
在楼上大佬代码的基础上优化了下,去重判断以题干文字
[Python] 纯文本查看 复制代码
import re
import os
import tkinter as tk
from tkinter import filedialog

# 获取程序所在目录
script_dir = os.path.dirname(os.path.abspath(__file__))

# 文件路径定义(默认设置为程序所在目录)
root = tk.Tk()
root.withdraw()  # 隐藏主窗口

# 选择输入文件(默认打开程序所在目录)
input_file = filedialog.askopenfilename(
    initialdir=script_dir,
    title="选择题目文件",
    filetypes=(("文本文件", "*.txt"), ("所有文件", "*.*"))
)
root.destroy()

if not input_file:
    print("未选择文件,程序终止。")
    exit()

output_dir = script_dir  # 输出目录设置为程序所在目录

# 从输入路径提取文件名(带扩展名)
input_basename = os.path.basename(input_file)
# 分割文件名和扩展名(支持多扩展名文件)
filename, ext = os.path.splitext(input_basename)
# 构建新文件名
output_file = os.path.join(output_dir, f"{filename}_去重处理后{ext}")

# 正则表达式匹配题号:数字+点开头
question_start_pattern = re.compile(r'^(\d+)(?:\.|\、)')
seen_questions = set()  # 改为存储题干指纹
questions = []
current_question = []

def is_question_start(line):
    return question_start_pattern.match(line.strip())

# 读取文件并分块提取题目(ANSI 编码 = gbk)
with open(input_file, 'r', encoding='utf-8') as f:
    for line in f:
        line = line.rstrip()  # 去除右边空格和换行符
        if is_question_start(line):
            if current_question:
                questions.append(current_question)
                current_question = []
        current_question.append(line)

    if current_question:
        questions.append(current_question)

# 处理题目:去空行、去重、规范格式
formatted_questions = []
for idx, lines in enumerate(questions, start=1):
    # 去掉题号前缀
    first_line = lines[0]
    # 生成题干指纹(去除题号+首行内容)
    clean_first_line = question_start_pattern.sub('', first_line, count=1).strip()
    # 构建新题干(去掉原题号,并重新编号为“1、”)
    if clean_first_line in seen_questions:
        continue
        
    seen_questions.add(clean_first_line)
   
    # 构建新题目
    new_question = [f"{idx}、{clean_first_line}"]
    for line in lines[1:]:
        stripped = line.strip()
        if stripped:  # 忽略空行
            new_question.append(line)

    # 添加空行在题干之后
    if len(new_question) > 0:
        new_question.insert(1, '')  # 第二行插入空行

    # 转为字符串用于去重
    question_str = '\n'.join(new_question)
    if question_str not in seen_questions:
        seen_questions.add(question_str)
        formatted_questions.append(new_question)

# 写入结果文件(使用 UTF-8 编码)
os.makedirs(output_dir, exist_ok=True)
with open(output_file, 'w', encoding='utf-8') as f:
    for q in formatted_questions:
        f.write('\n'.join(q))
        f.write('\n')  # 每个题目之间保留一个换行
# 输出重复的题目数量
print(f"处理完成,剔除重复题目 {len(questions) - len(formatted_questions)} 个,输出 {len(formatted_questions)} 道题目。")
# 修改最后的输出提示
print(f"输出文件:{output_file}")
uo31487 发表于 2025-6-7 22:19
去重思路,对比文件a ,文件b   1. 打开文件a , 文件b,  用两个循环,里面和外面的对比。  2,判断数据是否一致,写入C文件。 大概应该就是这个思路。。。
teddymvs 发表于 2025-6-7 23:01
本帖最后由 teddymvs 于 2025-6-7 23:03 编辑

这是给AI的结果。链接里面的结果.txt是我运行的结果,我看了下基本符合。
https://scuguyi.lanzout.com/i9Tae2yaysxc
这是代码,其实比较简单,交给AI就行
我的描述如下
写py程序处理txt文本去重和格式化。
处理文件,E:\pythoning\file\测试.txt。输出结果到‘D:\UserData\Downloads’下,命名为‘结果.txt’
处理方式:数据主要是题目 选项和答案组成。因此通过行读取:‘1.’到‘2.’之间为一道题目的完整内容。注意读取时需要注意:1.题号可能不规则,比如‘1.’后面是‘3.’,‘4.’,后面是‘10.’。因此要通过通配符识别‘数字.’开头的逻辑
2.获取分后的文本(按照获取的题目顺序排序),去除每一道题目中可能的空行。
3.之后再规范格式(识别是否重复,去重)
3.1去除每到题目的原本的‘1.’‘3.’等编号,按照‘1、’‘2、’重新依次编号。
3.2对题目行后面(每道题的第一行)再增加一个空行即可
[Python] 纯文本查看 复制代码
import re
import os

# 文件路径定义
input_file = r'E:\pythoning\file\测试.txt'
output_dir = r'D:\UserData\Downloads'
output_file = os.path.join(output_dir, '结果.txt')

# 正则表达式匹配题号:数字+点开头
question_start_pattern = re.compile(r'^(\d+)(?:\.|\、)')
seen_questions = set()
questions = []
current_question = []

def is_question_start(line):
    return question_start_pattern.match(line.strip())

# 读取文件并分块提取题目(ANSI 编码 = gbk)
with open(input_file, 'r', encoding='gbk') as f:
    for line in f:
        line = line.rstrip()  # 去除右边空格和换行符
        if is_question_start(line):
            if current_question:
                questions.append(current_question)
                current_question = []
        current_question.append(line)

    if current_question:
        questions.append(current_question)

# 处理题目:去空行、去重、规范格式
formatted_questions = []
for idx, lines in enumerate(questions, start=1):
    # 去掉题号前缀
    first_line = lines[0]
    clean_first_line = question_start_pattern.sub('', first_line, count=1).strip()

    # 构建新题干(去掉原题号,并重新编号为“1、”)
    new_question = [f"{idx}、{clean_first_line}"]
    for line in lines[1:]:
        stripped = line.strip()
        if stripped:  # 忽略空行
            new_question.append(line)

    # 添加空行在题干之后
    if len(new_question) > 0:
        new_question.insert(1, '')  # 第二行插入空行

    # 转为字符串用于去重
    question_str = '\n'.join(new_question)
    if question_str not in seen_questions:
        seen_questions.add(question_str)
        formatted_questions.append(new_question)

# 写入结果文件(使用 UTF-8 编码)
os.makedirs(output_dir, exist_ok=True)
with open(output_file, 'w', encoding='utf-8') as f:
    for q in formatted_questions:
        f.write('\n'.join(q))
        f.write('\n\n')  # 每个题目之间保留两个换行

print(f"✅ 处理完成,共输出 {len(formatted_questions)} 道不重复题目。")
print(f"📁 输出路径:{output_file}")
zhuguiyong 发表于 2025-6-7 23:22
https://wwus.lanzouu.com/iUv0t2yb0mpa
密码:6214
试试
 楼主| wangsheng518 发表于 2025-6-7 23:56
teddymvs 发表于 2025-6-7 23:01
这是给AI的结果。链接里面的结果.txt是我运行的结果,我看了下基本符合。
https://scuguyi.lanzout.com/i9 ...

想问一下 如果有同样的题目  另一个答案是正确的 他会显示在下面吗
1、当前现网商用最高网络制式是什么?
WCDMA(不选)
LTE(选)
NR(不选)
GSM(不选)
太遗憾了,答错了我的得分:0分
NR(选)
恭喜你,答对了我的得分:10分
teddymvs 发表于 2025-6-8 09:12
应该会显示,因为判断逻辑是 划分为每到题目后,是根据全部文本进行对比。所以有差异的话就是不同题目,你可以增加判断逻辑:题目去重的时候,先去除最后一行再进行对比去重。
 楼主| wangsheng518 发表于 2025-6-8 13:38
teddymvs 发表于 2025-6-8 09:12
应该会显示,因为判断逻辑是 划分为每到题目后,是根据全部文本进行对比。所以有差异的话就是不同题目,你 ...

好的 我试试 谢谢
 楼主| wangsheng518 发表于 2025-6-8 13:39
zhuguiyong 发表于 2025-6-7 23:22
https://wwus.lanzouu.com/iUv0t2yb0mpa
密码:6214
试试

这个还要手动操作 谢谢 希望自动识别输出文件  如果文件名重复 自动叠加 如 hua1 hua2 hua3 自动识别
 楼主| wangsheng518 发表于 2025-6-8 13:53
teddymvs 发表于 2025-6-8 09:12
应该会显示,因为判断逻辑是 划分为每到题目后,是根据全部文本进行对比。所以有差异的话就是不同题目,你 ...

测试了 不是在下面显示的 还是分开的  重新一个序号 题目和答案都在
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - 52pojie.cn ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2026-10-24 03:17

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表