小白教程

 找回密码
 立即注册
查看: 7904|回复: 3

未能打印第二个文件中未匹配的行

[复制链接]

4

主题

5

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2021-5-21 04:39:41 | 显示全部楼层 |阅读模式
出于某种原因,我没有得到Python的迭代。我有两个文件,文件1和文件-2。我必须使用文件-1行在文件-2中查找行。
File-1 lines:

03/28/2021,P,6,LINE2

03/28/2021,P,9,LINE4


File-2 lines:

03/28/2021,P,16,LINE1

03/28/2021,P,6,LINE2

03/28/2021,P,9,LINE3

03/28/2021,P,9,LINE4

03/28/2021,P,8,LINE5

03/28/2021,S,95,LINE6

03/28/2021,S,1,LINE7

03/28/2021,P,46,LINE8


I need to print out only lines that do not match:

03/28/2021,P,16,LINE1

03/28/2021,P,9,LINE3

03/28/2021,P,8,LINE5

03/28/2021,S,95,LINE6

03/28/2021,S,1,LINE7

03/28/2021,P,46,LINE8

But the code I wrote prints this:

ELSE -->> 03/28/2021,P,16,LINE1

ELSE -->> 03/28/2021,P,9,LINE3

ELSE -->> 03/28/2021
, P,9,LINE4 ELSE -- >> 03/28/2021,P,8,LINE5

ELSE -- >> 03/28/2021,S,95,LINE6

ELSE - >>03/28/2021,S,1,LINE7

ELSE -- >> 03/28/2021,P,46,LINE8

ELSE -- >>03/28/2021,P,16,LINE1

ELSE - >>03/28/2021,P,6,LINE2

ELSE -- >> 03/28/2021,P,9,LINE3

ELSE -- >> 03/28/2021,P,8,LINE5

ELSE - >>03/28/2021,S,95

,LINE6 ELSE - >> 03/28/2021,S,1,LINE7

ELSE - >>03/28/2021,P,46,LINE8

我必须在文件-1和文件-2中拆分线,在行中需要一些额外的处理。
以下是代码:
  1. with open (file_2,'r') as l_few :
  2.     f2=l_few.readlines()
  3. with open (file_1,'r') as f1:         
  4.     for lf1 in f1:
  5.         lf1=lf1.strip()
  6.         sp1 = lf1.split(",")
  7.         for lf2 in f2 :
  8.             lf2=lf2.strip()
  9.             if lf2 :
  10.                 if sp1[3] in lf2 :
  11.                     spL2=lf2.split(",")
  12.                     #print (" File 2 Line matched --> "+lf2)
  13.                     #break
  14.                 else :
  15.                     print (" ELSE -->> "+lf2)
  16.                     #break
复制代码

回复

使用道具 举报

0

主题

2

帖子

3

积分

新手上路

Rank: 1

积分
3
发表于 2021-5-22 08:40:11 | 显示全部楼层
首先,"l_few"(行数较少的文件)根据文本进行file_1,因此我交换了文件名称,以便在原始程序中尽可能少地更改。
然后,我想出这个,希望它有助于,请注意,我没有太大的变化,只是足以让它打印你需要的:
  1. with open ('file_2','r') as l_few :
  2.     f2=l_few.readlines()
  3. sp23 = [s.split(',')[3].strip() for s in f2] # collect all the terms you need to compare
  4. with open ('file_1','r') as f1:         
  5.     for lf1 in f1:
  6.         lf1=lf1.strip()
  7.         sp1 = lf1.split(",")
  8.         if sp1[3] in sp23:
  9.             pass # here we have a match so print nothing
  10.             # spL2=lf2.split(",")
  11.             #print (" File 2 Line matched --> "+lf2)
  12.             #break
  13.         else :
  14.             print (lf1) # print the non-matching line
  15.             #break
复制代码
回复

使用道具 举报

0

主题

2

帖子

14

积分

新手上路

Rank: 1

积分
14
发表于 2021-5-27 11:45:59 | 显示全部楼层
我无法直接比较文件。我需要处理行后,在这两个文件中的行将不分。
回复

使用道具 举报

0

主题

5

帖子

10

积分

新手上路

Rank: 1

积分
10
发表于 2021-6-1 13:33:40 | 显示全部楼层
仍然可以使用一套。它将比比较字符串更有效。首先构建您正在寻找的一组字符串,然后在第二个文件中读取并检查与设置。
  1. with open(DIR/'lines_to_look_for.txt', 'r') as file:
  2.     lines = set([line.strip().split(',')[3] for line in file])

  3. with open(DIR/'check_for_lines.txt', 'r') as file:
  4.     for line in file:
  5.         line = line.strip()
  6.         if line.split(',')[3] in lines:
  7.             print(f'{line} MATCH')
  8.         else:
  9.             print(f'{line} NO MATCH')
复制代码
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|小白教程 ( 粤ICP备20019910号 )

GMT+8, 2024-9-20 12:32 , Processed in 0.029594 second(s), 26 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc. Template By 【未来科技】【 www.wekei.cn 】

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