python对比两个txt文件不同,在python中逐行比较两个不同的文件

I have two different files and I want to compare theirs contents line by line, and write their common contents in a different file. Note that both of them contain some blank spaces.

Here is my pseudo code:

file1 = open('some_file_1.txt', 'r')

file2 = open('some_file_2.txt', 'r')

FO = open('some_output_file.txt', 'w')

for line1 in file1:

for line2 in file2:

if line1 == line2:

FO.write("%s\n" %(line1))

FO.close()

file1.close()

file2.close()

However, by doing this, I got lots of blank spaces in my FO file. Seems like common blank spaces are also written. I want to write only the text part. Can somebody please help me.

For example: my first file (file1) contains data:

Config:

Hostname = TUVALU

BT:

TS_Ball_Update_Threshold = 0.2

BT:

TS_Player_Search_Radius = 4

BT:

Ball_Template_Update = 0

while second file (file2) contains data:

Pole_ID = 2

Width = 1280

Height = 1024

Color_Mode = 0

Sensor_Scale = 1

Tracking_ROI_Size = 4

Ball_Template_Update = 0

If you notice, last two lines of each files are the same, hence, I want to write this file in my FO file. But, the problem with my approach is that, it writes the common blank space also. Should I use regex for this problem? I do not have experience with regex.

解决方案

This solution reads both files in one pass, excludes blank lines, and prints common lines regardless of their position in the file:

with open('some_file_1.txt', 'r') as file1:

with open('some_file_2.txt', 'r') as file2:

same = set(file1).intersection(file2)

same.discard('\n')

with open('some_output_file.txt', 'w') as file_out:

for line in same:

file_out.write(line)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值