在python中找到一个文本文件在另一个文件中的条目 [英] Find entries of one text file in another file in python
本文介绍了在python中找到一个文本文件在另一个文件中的条目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个文件。文件 A 每行都有一些条目,我需要确定在文件 B 中是否找到任何条目。这是我的脚本(使用两个函数):
I have two files. File A has some entries in each line and I need to find if any entry is found in File B. Here is my script (using two functions):
def readB(x):
with open('B.txt') as resultFile:
for line in resultFile:
if x in line:
print x
def readA():
with open('A.txt') as bondNumberFile:
for line in bondNumberFile:
readB(line)
readA()
此脚本在第二个文件中找到第一个条目,然后找不到下一个。
This script finds the first entry in second file and then does not finds the next one. What might be wrong here?
文件A看起来像这样:
122323
812549
232335
921020
文件B看起来像这样:
696798 727832 750478 784201 812549 838916 870906 890988 921020
697506 727874 751037 784955 813096 838978 872494 891368 921789
696798 727832 750478 784201 812549 838916 870906 890988 921020
697506 727874 751037 784955 813096 838978 872494 891368 921789
推荐答案
将换行符的条带化
读取行时Python包含换行符-您的第一个条目读为 1223232\n
。删除换行符,它将起作用。
Strip the entries of newlines
Python includes newlines when you read lines - your first entry is read as 1223232\n
. Strip the newline and it will work.
def readA():
with open('A.txt') as bondNumberFile:
for line in bondNumberFile:
readB(line.rstrip())
这篇关于在python中找到一个文本文件在另一个文件中的条目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文