|
出于某种原因,我没有得到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中拆分线,在行中需要一些额外的处理。
以下是代码:
- with open (file_2,'r') as l_few :
- f2=l_few.readlines()
- with open (file_1,'r') as f1:
- for lf1 in f1:
- lf1=lf1.strip()
- sp1 = lf1.split(",")
- for lf2 in f2 :
- lf2=lf2.strip()
- if lf2 :
- if sp1[3] in lf2 :
- spL2=lf2.split(",")
- #print (" File 2 Line matched --> "+lf2)
- #break
- else :
- print (" ELSE -->> "+lf2)
- #break
复制代码
|
|