逐行比较2个文件(不含列表)

时间:2018-09-28 16:03:38

标签: python python-3.x

对于这个项目,我想比较来自单独文件的两行,在Python中一个接一个字符。我无法使程序返回或打印任何内容。该程序需要执行以下操作:

  • 从每个文件中读取下一行

for line1 in currentLine1: for line2 in currentLine2:

  • 确定行的长度是否不同

                if len.line1 != len.line2:  # If the line lengths are not equal return the line number
                return charByChar(count=count + 1, differenceCounter=differenceCounter, textCount1=textCount1,
                                  textCount2=textCount2, difLineCounter=difLineCounter)
    
  • 如果行长相同,请比较字符

                if len.line1 == len.line2:  # If the lines lengths are equal
                    for char in range(len(line1)):  # Compare line by line
    
  • 如果它们匹配,则不执行任何操作。但是,如果它们确实匹配,则打印不匹配的字符(1);使用N:M格式打印行号(2);处理下一行(3)。

                        if line1[char] != line[char]:  # If the lines have different characters
                            print("Unmatched characters")
                            print("Line number:", count)
                            print("First different character", char)
                            differenceCounter = difLineCounter + 1  # add 1 to the difference counter
                            textCount1 = textCount1 + 1
                            textCount2 = textCount2 + 1
                    return charByChar(count=count, differenceCounter=differenceCounter, textCount1=textCount1,
                                  textCount2=textCount2)  # return difference count
    
  • 最后打印出第一个文件中的字符数(1);第二个文件中的字符数(2);相同长度的行中不匹配的字符数( 3);长度不同的行数(4)。

                if len.line1 == 0 or len.line2 == 0:
                print("Extra lines are not matching")
            **Some print then return statement**
    

我相信我已经掌握了所有这些内容,但是,我的代码从不返回任何值。它总是以退出代码0结尾(我已经引用了2个文本文件进行比较)。

对于上下文,这是我编写的所有代码

text1 = open("file1.txt", "r")
text2 = open("file2.txt", "r")
content1 = text1.read()
content2 = text2.read()


def charByChar(count=0, differenceCounter=0, textCount1=0, textCount2=0, difLineCounter=0):
    currentLine1 = content1
    currentLine2 = content2
    line = text1.readline(count)
    if line != '':
        for line1 in currentLine1:
            for line2 in currentLine2:
                if len.line1 != len.line2:  # If the line lengths are not equal return the line number
                    return charByChar(count=count + 1, differenceCounter=differenceCounter, textCount1=textCount1,
                                      textCount2=textCount2, difLineCounter=difLineCounter)
                if len.line1 == len.line2:  # If the lines lengths are equal
                    for char in range(len(line1)):  # Compare line by line
                        if line1[char] != line[char]:  # If the lines have different characters
                            print("Unmatched characters")
                            print("Line number:", count)
                            print("First different character", char)
                            differenceCounter = difLineCounter + 1  # add 1 to the difference counter
                            textCount1 = textCount1 + 1
                            textCount2 = textCount2 + 1
                    return charByChar(count=count, differenceCounter=differenceCounter, textCount1=textCount1,
                                      textCount2=textCount2)  # return difference count
                if len.line1 == 0 or len.line2 == 0:
                    print("Extra lines are not matching")
    text2.close()
    text1.close()


def main():
    charByChar()


main()

1 个答案:

答案 0 :(得分:0)

您的函数charByChar()似乎没有返回基本情况。例如,在记录额外行不匹配的地方,请尝试返回False-1或返回的内容。

相关问题