【发布时间】:2018-03-01 23:09:54
【问题描述】:
我有 2 个包含多个字符串的文件,fileA.txt 和 fileB.txt。
文件A.txt:
hello hi
how
文件B.txt:
hello how are you
我正在尝试编写一个程序来查看两个文件中是否存在字符串。如果是,则打印字符串或多个字符串。
结果将打印“hello”和“how”,因为它们存在于两个文件中。
我无法执行此操作,因为我只能使用我定义的字符串,而不是文件中的未知字符串:
with open("fileA.txt", 'r') as fileA, open ("fileB.txt") as fileB:
for stringsA in fileA:
for stringsB in fileB:
if stringsA in stringsB:
print("true")
任何帮助将不胜感激。
【问题讨论】:
标签: python python-3.x file