【问题标题】:Compare 2 files line by line without list无列表逐行比较 2 个文件
【发布时间】:2018-09-28 16:03:38
【问题描述】:

对于这个项目,我想在 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()

【问题讨论】:

  • 也许这是一个错字或者我遗漏了一些东西,但是当您检查行长度是否相等时,如果它们的长度不同,您将调用 CharByChar。这是故意的吗?
  • 递归中没有基本情况。
  • 您是在为另一个项目构建此功能还是更像是一个练习项目?因为我相信docs.python.org/3/library/filecmp.html 已经做了你想做的事
  • @JChao 我的方法有限

标签: python python-3.x


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2013-03-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-11-26
    • 2021-04-27
    • 2017-04-06
    • 1970-01-01
    • 2015-12-02
    相关资源
    最近更新 更多