我正在尝试比较Python中的两个文件A和C,由于某些原因,for循环的双重似乎没有正常工作:
with open(locationA + filenameC,'r') as fileC, open(locationA + filenameA,'r') as fileA:
for lineC in fileC:
fieldC = lineC.split('#')
for lineA in fileA:
fieldA = lineA.split('#')
print 'UserID Clicks' + fieldC[0]
print 'UserID Activities' + fieldA[0]
if (fieldC[0] == fieldA[0]) and (fieldC[2] == fieldA[2]):
print 'OK'
这里,似乎只比较了C行,但对于其他行," A循环"似乎被忽略了。
任何人都可以帮我吗?
答案 0 :(得分:1)
您的问题是,一旦您需要再次将指针更改为文件的开头,则迭代fileA
。
所以你可能会做的是从两个文件创建两个列表并根据需要迭代它们多次。例如:
fileC_list = fileC.readlines()
fileA_list = fileA.readlines()
for lineC in fileC_list:
# do something
for lineA in fileA_list:
# do somethins
答案 1 :(得分:0)
嵌套循环的问题(从当前问题的角度来看)正是内循环在外循环的每次迭代中运行完成。因此,通过显式调用lineA
迭代器中的下一个项来设置fileA
:
with open(locationA + filenameC,'r') as fileC, open(locationA + filenameA,'r') as fileA:
for lineC in fileC:
fieldC = lineC.split('#')
lineA = next(fileA)
fieldA = lineA.split('#')
print 'UserID Clicks' + fieldC[0]
print 'UserID Activities' + fieldA[0]
if (fieldC[0] == fieldA[0]) and (fieldC[2] == fieldA[2]):
print 'OK'
一旦fileA
耗尽,此逻辑将忽略来自fileC
的任何额外行,如果fileC
包含的行数多于FileA
,则在没有特殊检查的情况下,事情也可能变得难看。
另一种方法可能会使用itertools.izip()
成对收集每个文件中的行:
import itertools
with open(locationA + filenameC,'r') as fileC, open(locationA + filenameA,'r') as fileA:
for lineC, lineA in itertools.izip(fileC, fileA):
fieldC = lineC.split('#')
fieldA = lineA.split('#')
print 'UserID Clicks' + fieldC[0]
print 'UserID Activities' + fieldA[0]
if (fieldC[0] == fieldA[0]) and (fieldC[2] == fieldA[2]):
print 'OK'
我无法想到使用一个而不是另一个的任何特定原因,但如果文件任何大小都拒绝使用内置zip()
函数而不是{{1前者返回一个列表,因此内存使用量取决于文件大小,而后者是生成器,因此在需要时创建值。
答案 2 :(得分:0)
您正在将FileA中的所有行与FileC中的每一行进行比较。这意味着,对于文件C的每一行,您将读取整个FileA,并且(如果您将指针移动到文件A的开头),您将再次阅读它。
在它们都有线条的同时更容易同时读取它们 如果他们是相同的,做一些事情,从两个阅读
如果它们不同,则从最小的读取(线A<线C,仅从文件A读取;线C<线A,仅从线C读取)
并且在剩下的行中进行两个最后的循环(两个循环,每个文件一个,因为你不知道哪一个用完了行)
答案 3 :(得分:0)
我知道这是一个旧帖子,但当有人正在寻找比较python中的2个文本文件的解决方案时,它会出现在google上。
这段代码对我有用。
您可以更新代码并使用"打开"而是根据你的喜好进行微调,但它能完成这项任务。
# Ask the user to enter the names of files to compare
fname1 = input("Enter the first filename (text1.txt): ")
fname2 = input("Enter the second filename (text1.txt): ")
# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)
# Print confirmation
print("-----------------------------------")
print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
print("-----------------------------------")
# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()
# Initialize counter for line number
line_no = 1
# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':
# Strip the leading whitespaces
f1_line = f1_line.rstrip()
f2_line = f2_line.rstrip()
# Compare the lines from both file
if f1_line != f2_line:
# If a line does not exist on file2 then mark the output with + sign
if f2_line == '' and f1_line != '':
print(">+", "Line-%d" % line_no, f1_line)
# otherwise output the line on file1 and mark it with > sign
elif f1_line != '':
print(">", "Line-%d" % line_no, f1_line)
# If a line does not exist on file1 then mark the output with + sign
if f1_line == '' and f2_line != '':
print("<+", "Line-%d" % line_no, f2_line)
# otherwise output the line on file2 and mark it with < sign
elif f2_line != '':
print("<", "Line-%d" % line_no, f2_line)
# Print a blank line
print()
#Read the next line from the file
f1_line = f1.readline()
f2_line = f2.readline()
#Increment line counter
line_no += 1
# Close the files
f1.close()
f2.close()