简体   繁体   English

检查txt文件中是否有新字符串

[英]Check if there are new strings in a txt file

I am trying to make a function which will compare two txt files. 我正在尝试制作一个将比较两个txt文件的函数。 If it recognizes new lines that are in one file but not in the other, it will add them in a list and also in that file that does not contain those new lines. 如果它识别一个文件中的换行符,但不能识别另一个文件中的换行符,则会将它们添加到list ,也可以添加到不包含这些新行的文件中。 It fails to do that. 它无法做到这一点。 Here is my function. 这是我的功能。 What am I doing wrong? 我究竟做错了什么?

newLinks = []

def newer():
with open('cnbcNewLinks.txt', 'w') as newL:
    for line in open('cnbcCleanedLinks.txt'):
        if line not in "cnbcNewLinks.txt":
            newLinks.append(line)
            newL.write(line)
        else:
            continue
cleaned = ''.join(newLinks)
print(cleaned)

If files not big, then move data in list, both of list convert in set and use 'differ' builtin functions, two times. 如果文件不大,则将数据移动到列表中,列表中的两个都将转换为set并使用两次“ differ”内置函数。 then add difference in files. 然后在文件中添加差异。

I put in python code what @Alex suggested. 我输入了@Alex建议的python代码。

See the doc for set . 请参阅文档set

I replace you text file name by a.txt and b.txt to be easily readable. 我将文本文件名替换为a.txtb.txt以便于阅读。

# First read the files and compare then using `set`
with open('a.txt', 'r') as newL, open('b.txt', 'r') as cleanL:
    a = set(newL)
    b = set(cleanL)
    add_to_cleanL = list(a - b) # list with line in newL that are not in cleanL
    add_to_newL = list(b - a) # list with line in cleanL that are not in newL

# Then open in append mode to add at the end of the file
with open('a.txt', 'a') as newL, open('b.txt', 'a') as cleanL:
    newL.write(''.join(add_to_newL)) # append the list at the end of newL
    cleanL.write(''.join(add_to_cleanL)) # append the list at the end of cleanL

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 python 读取文本文件并添加字符串并创建新的txt文件 - python read text file and add strings and create new txt file Python在txt文件中查找字符串以特定格式写入新文件 - Python Find Strings in txt file writing in new file in specific form 根据字符串列表递归检查txt文件的内容 - Recursively check txt file contents against a list of strings 如何通过用户输入检查txt文件中是否存在多个字符串? - How to check if multiple strings are present in a txt file through user input? 检查列表中的元素是否存在于 txt 文件的字符串中(不工作) Python - check if an element from a list exists in a strings of a txt file (not working) Python 列表和字符串格式设置的困难:导入txt文件,添加字符串并将其写入新文件 - List and String formatting difficulties: importing a txt file, adding strings and writing it to a new file 从.txt文件读取时如何检查两个字符串是否在同一行上 - How to check if two strings are on the same line when reading from a .txt file 如何在Python代码中传递txt文件中的新行以将函数应用于不同的字符串? - How to pass new lines in txt file in Python code to apply function to different strings? 打开具有3列的txt文件,条件是检查第3列的差异是否为5,并将该行的第1列和第2列返回到新的txt文件中 - open txt file with 3 columns ,with a condition to check the 3rd column for difference of 5 and return that row's column 1 and 2 to a new txt file 如何检查txt文件的内容 - How to check txt file for content
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM