Python如果行包含来自另一个文件的字符串,如何打印行?
文本文件:
test.com
test2.com
test3.com
另一个文本文件:
test.com:user:pass
test3.com:user:pass
test9.com:user:pass
(我从两个文件中得到两个列表)
a = ['test1.com,user,pass','test2.com,user,passw','tes4.com,user,pass']
b = ['test5.com','test1.com','test2.com']
temp = a[:]
for i in range(len(a)):
temp[i] = temp[i].split(':')[0]
for i in b:
if i in temp:
print i
答案 0 :(得分:0)
a = ['test1.com,user,pass','test2.com,user,passw','tes4.com,user,pass']
b = ['test5.com','test1.com','test2.com']
temp = a[:]
for i in range(len(a)):
temp[i] = temp[i].split(',')[0]
for i in range(len(temp)):
if temp[i] in b:
print a[i]
这将根据需要打印其他列表的内容
test1.com,用户,通过
test2.com,用户,PASSW
答案 1 :(得分:0)
使用列表理解:
a = ['test1.com,user,pass','test2.com,user,passw','tes4.com,user,pass']
b = ['test5.com','test1.com','test2.com']
a = [i.split(',') for i in a]
final_list = [''.join(i) for i in a if i[0] in b]
print final_list
现在为您提供站点,用户名和密码(如果该站点存在于列表b
中)