網(wǎng)站首頁 編程語言 正文
# -*- coding: UTF-8 -*-
import os
from pathlib import Path
'''
A對比B 將A與B的交集或并集等結(jié)果輸出到一個文件中
注意格式必須一致 按行讀取 必要時 在EXCEL統(tǒng)一下格式
intersection
'''
pathA = Path(r'C:\Users\EDZ\Desktop\A.txt')#獲取A文件路徑 A和B需要手動先創(chuàng)建txt
pathB = Path(r'C:\Users\EDZ\Desktop\B.txt') # 獲取B文件路徑
pathresult = Path(r'C:\Users\EDZ\Desktop\result.txt')#結(jié)果路徑 result.txt是自動生成
'''
讀取文件A
'''
#目錄臨時限制死 后改為輸入形式并抓異常
def read_file_A():
with open(pathA,mode='r', encoding='utf-8') as f: #2.讀取文件
content = f.readlines()
Aset = set(content) #3.將文件按行存入setA中 注意空格與換行符
return Aset
#print(Aset)
'''
讀取文件B
'''
def read_file_B():
#目錄臨時限制死 后改為輸入形式并抓異常
with open(pathB,mode='r', encoding='utf-8') as f: # 2.讀取文件
content = f.readlines()
Bset = set(content) # 3.將文件按行存入setA中 注意空格與換行符
return Bset
# print(Bset)
'''
比較
'''
def compare_file():
A_Set = read_file_A()
B_Set = read_file_B()
output_comparefile_result(A_Set, B_Set)
print('完成')
'''
輸出文件到指定位置
'''
#目錄臨時限制死
def output_comparefile_result(A_set,B_set):
input_menu = input('菜單:輸入1或2或3\n1.查出A和B相同的\n2.查出A存在B不存在的\n3.查出B存在A不存在的\n')
with open(pathresult, 'w', encoding="utf-8") as f:
if(input_menu=='1'):
str=A_set.intersection(B_set) #查出相同的
elif(input_menu=='2'):
str=A_set.difference(B_set) #查出不同的
elif(input_menu=='3'):
str = B_set.difference(A_set) # 查出不同的
else:
raise ValueError('Error,發(fā)生異常,只能輸入1、2、3')
for x in str:
f.write(x)
#程序入口
if __name__ == '__main__':
try:
compare_file()
except Exception as e:
print(e)
原文鏈接:https://blog.csdn.net/weixin_48045037/article/details/125664811
相關(guān)推薦
- 2022-09-25 Django 使用定時任務(wù)
- 2022-06-28 ES6基礎(chǔ)語法之Map和Set對象_基礎(chǔ)知識
- 2022-10-03 React?Native?中實現(xiàn)倒計時功能_React
- 2022-05-10 C++構(gòu)造函數(shù)+復(fù)制構(gòu)造函數(shù)+重載等號運算符調(diào)用_C 語言
- 2022-03-31 用C語言實現(xiàn)排雷游戲_C 語言
- 2022-03-19 基于React?Hooks的小型狀態(tài)管理詳解_React
- 2022-05-05 Python+OpenCV實現(xiàn)角度測量的示例代碼_python
- 2022-11-10 C++在多線程中使用condition_variable實現(xiàn)wait_C 語言
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支