当前位置:首页 » 编程语言 » python比较文件

python比较文件

发布时间: 2022-06-11 19:47:01

A. python如何把txt文件进行对比提取唯一数据

我写了个简单的实现方式,稍微修改就能用了,你可以看看:

代码:

a = [1, 2, 3] # 读取的 a.txt
b = [1, 2, 3, 4] # 读取的 b.txt
c = []
for t in b:
if t in a:
pass
else:
c.append(t) # 写入c.txt

print(c)

B. Python比较两个文件是否相同,倒数第二行不太明白

答: 确实是有道理的,在文件当中的第7行,实现的功能就是从第1个文件中取一个字符,而第8行是在第2个文件中取一行字符,那么一个字符和一行字符比较肯定是不相等的呢,所以我觉得第7行那个for循环改一下,改成每次取一行,然后一行和一行比较当比较,结果不同时记录下它的行号,得到最后的结果。

希望可以帮助到你!

C. 如何用Python代码实现自动比较两个文件中的代

可以用 difflib库,下面给一个例子,具体需求自己研究
假如在同一个目录下有a.txt, b.txt 两个文本文件
a.txt 内容是
aaa
bbb
b.txt内容是
aaa
ccc
1234567import difflib a = open('a.txt', 'U').readlines()b = open('b.txt', 'U').readlines()diff = difflib.ndiff(a, b) sys.stdout.writelines(diff)
结果是:
aaa
- bbb+ ccc

D. python如何将两个文件价中所有同名excel秋裤对比

使用xlrd和xlwt包,首先安装这两个包。定义contrast函数,测试contrast函数,把程序打包成exe文件。导入tkinter包,写个函数用来选择路径,初始化变量,画出UI界面,点击对比按钮后的函数。
接下来就是把这个py程序打包,使用pyinstaller这个包pipinstallpyinstaller。安装成功之后,按键盘win+R打开运行,输入cmd,回车运行。进入程序所在文件夹
因为有统计成员到会情况的任务,每次汇总时都很麻烦,需要一个个对应腾讯会议导出名单的成员,然后在总表上进行标记,所以就写了本程序来减少统计的复杂度。

E. 使用Python实现比较俩个文件的数据,不同的存在另一个文件里

这是我之前在excel中比较两组不同数据的代码,修改一下完全可以满足你的要求。

#-*-coding:utf-8-*-
importxlrd
importxlwt
fromxlutils.import
importos,time
importoperator

path=r"E:xx"
#path=raw_input('InputPath:')
os.chdir(path)
print"CurrentWorkspace:%s"%os.getcwd()

#读取excel工作表中数据为字典
#字典形式为:{代码:地名}
defreadDictStandard():
#name_check=raw_input('CheckExcelName:')
filename=(name_check).decode('cp936')
data=xlrd.open_workbook(filename+'.xls',formatting_info=True)

table=data.sheet_by_index(0)
#table=data.sheet_by_name(u'di')
printtable.name

cellList_k=[]
cellList_v=[]
ncols=table.ncols
forcolinrange(0,ncols):
ifnot(col%2):
collist_k=table.col_values(col)
collist_v=table.col_values(col+1)
forcell_kincollist_k:
cellList_k.append(cell_k)
forcell_vincollist_v:
cellList_v.append(cell_v)

check=dict(zip(cellList_k,cellList_v))

num=0
forkeyincheck:
num+=1
#printstr(key),check[key]

print'%nitsincheckExcel'%num
print'-'*50
returncheck

defreadDictCheck():
#name_check=raw_input('CheckExcelName:')
filename=(name_check).decode('cp936')
data=xlrd.open_workbook(filename+'.xls',formatting_info=True)

table=data.sheet_by_index(0)
#table=data.sheet_by_name(u'sheet1')
printtable.name

cellList_k=[]
cellList_v=[]
ncols=table.ncols

collist_k=table.col_values(0)
collist_v=table.col_values(1)
forcell_kincollist_k:
cellList_k.append(cell_k)
forcell_vincollist_v:
cellList_v.append(cell_v)

check=dict(zip(cellList_k,cellList_v))

num=0
forkeyincheck:
num+=1
#printstr(key),check[key]

print'%nitsincheckExcel'%num
print'-'*50
returncheck

defcheckDict(check,standard):
num=0
forkinsorted(check.keys()):
ifknotinstandard.keys():
num+=1
printk,check[k]
elifcheck[k]!=standard[k]:
printk,check[k],standard[k]
num+=1
print'%dnumbersrecords'%num

defmain():
globalname_check
name_check=raw_input('CheckExcelName:')
check=readDictCheck()
name_check=raw_input('StandardExcelName:')
standard=readDictStandard()
time.sleep(1)
checkDict(check,standard)

if__name__=="__main__":
main()
print'-'*50

F. python读取两个文件并且判断是否一致

'''判断两个文件是否相同,如果不同请指出第几行不相同'''def f1vsf2(name1,name2):
f1 = open(name1)
f2 = open(name2)
count = 1
msg=[] for line1 in f1:
line2 = f2.readline() if(line1!=line2):
msg.append("第%d行不一样"%count)
count+=1
f1.close()
f2.close() return msg
isbool = Truewhile isbool:
fname1 = input("请输入要比较的文件1路径及文件名:") if fname1 =='': print("文件名不能请重新输入") break;
fname2 = input("请输入要比较的文件2路径及文件名:") if fname2 =='': print("文件名不能请重新输入") break;
result = f1vsf2(fname1,fname2) if len(result)==0: print("两个文件完全一致") else: print("两个文件共有【%d】行不同"%len(result)) for msg in result: print(msg)
isbool = False

G. python中怎么比较文件名字符与文件指定位置内容字符是否一致

代码逻辑:(1)打开文件;(2)读取文件内容:(3)找到第2行第3、4个字符;(4)判断是否与文件名相同。

代码如图:

H. python中怎么快速比较2个文件中的内容

可以用 difflib库,下面给一个例子,具体需求自己研究

假如在同一个目录下有a.txt, b.txt 两个文本文件

a.txt 内容是

aaa

bbb


b.txt内容是

aaa

ccc


importdifflib

a=open('a.txt','U').readlines()
b=open('b.txt','U').readlines()
diff=difflib.ndiff(a,b)

sys.stdout.writelines(diff)



结果是:

aaa

- bbb+ ccc

I. python比较文件中某些列的值

import glob
import fileinput
import io

filelist=glob.glob('a.txt')
for x in filelist:
read = open(x,'r',encoding = 'utf-8')
line = read.readline()
while(line!=''):
a=line.replace('\n','').split(' ')
if a[1] == a[2]:
print(line)#写入文件在这里写
line = read.readline()
read.close()

热点内容
超级脚本制作 发布:2025-02-07 19:31:30 浏览:484
怎么查看支付宝的账号密码 发布:2025-02-07 19:26:48 浏览:15
惠普服务器查看ip指令 发布:2025-02-07 19:26:47 浏览:433
算法设计模式 发布:2025-02-07 19:15:52 浏览:743
服务器1u能连接几台电脑 发布:2025-02-07 18:50:02 浏览:152
立人编译 发布:2025-02-07 18:48:32 浏览:764
日产途达四驱的有哪些配置 发布:2025-02-07 18:42:02 浏览:831
服务器搭建镜像站 发布:2025-02-07 18:41:55 浏览:376
游戏上云成标配云服务器该怎么选 发布:2025-02-07 18:26:13 浏览:141
哪个安卓手机自带系统没有广告 发布:2025-02-07 18:22:36 浏览:724