如何从 python 中的文件 2 中删除文件 1 的内容
how do I delete the content of file 1 from file 2 in python
我正在尝试用另一个文件删除第一个给定文件的内容。
这是因为第一个文件包含的数据也存在于第二个文件中。
然而,第二个文件比第一个文件包含更多和更新的数据。因此我想从第二个文件中删除第一个文件的内容。
我试过这个:
# file 1 needs to be compared with file 2
with open(path8 + "recent.txt") as timelog:
lines = timelog.read()
time = lines.split('\n', 1)[0]
print(time)
# file 1
finold = open(path7 + time + 'temp.csv', 'r')
# file 2
finnew = open(pathmain + "file2.csv", 'r')
copyfinnew = open(path7 + "tempfile1.csv", 'w')
fout = open(path7 + "tempfile2.csv", 'w')
for line in finnew:
copyfinnew.write(line)
lines = line
delete = lines.split('\n', 1)[0]
if not delete in finold:
print(delete, " not in finold")
fout.write(delete + '\n')
文件 1 是一个“日志”文件,自上次主代码(不是这个)有 运行 时自动保存。
文件 2 是尚未通过主代码 运行 的新文件,但需要从中删除文件 1 的内容,以便主代码通过 运行 的数据较少。
错误似乎是“if not delete in finold:”没有按照我的预期执行。
我希望文件 2 中的字符串去查看文件 1 中是否存在相同的字符串。它似乎确实循环正确,但是它 returns 文件 2 中的所有字符串。我在下面给出了我的意思的示例.
我希望这是有道理的,我希望有人能给出答案。
finold 的样本是:
Srno,Flex Mood,Montana,08 Apr 2022 00:02
and for finnew it would be:
Bryan Mg,Bryan,Ça Sert à Rien,08 Apr 2022 00:11
Glowinthedark,Lituatie,Anders,08 Apr 2022 00:08
Architrackz,Perfecte Timing,Oh Baby (Whine),08 Apr 2022 00:05
Srno,Flex Mood,Montana,08 Apr 2022 00:02
that means that an example of fout would be:
Bryan Mg,Bryan,Ça Sert à Rien,08 Apr 2022 00:11
Glowinthedark,Lituatie,Anders,08 Apr 2022 00:08
您正在尝试迭代打开的文件缓冲区。如果你想遍历每一行,你需要阅读它们并将它们分成几行,就像你在第一个代码块中所做的那样 timelog
。
像这样:
# file 1
finold = open(path7 + time + 'temp.csv').read().split("\n")
# file 2
finnew = open(pathmain + "file2.csv").read().split("\n")
copyfinnew = open(path7 + "tempfile1.csv", 'w')
fout = open(path7 + "tempfile2.csv", 'w')
for line in finnew:
copyfinnew.write(line)
if line not in finold:
print(line, " not in finold")
fout.write(line + '\n')
我正在尝试用另一个文件删除第一个给定文件的内容。 这是因为第一个文件包含的数据也存在于第二个文件中。 然而,第二个文件比第一个文件包含更多和更新的数据。因此我想从第二个文件中删除第一个文件的内容。
我试过这个:
# file 1 needs to be compared with file 2
with open(path8 + "recent.txt") as timelog:
lines = timelog.read()
time = lines.split('\n', 1)[0]
print(time)
# file 1
finold = open(path7 + time + 'temp.csv', 'r')
# file 2
finnew = open(pathmain + "file2.csv", 'r')
copyfinnew = open(path7 + "tempfile1.csv", 'w')
fout = open(path7 + "tempfile2.csv", 'w')
for line in finnew:
copyfinnew.write(line)
lines = line
delete = lines.split('\n', 1)[0]
if not delete in finold:
print(delete, " not in finold")
fout.write(delete + '\n')
文件 1 是一个“日志”文件,自上次主代码(不是这个)有 运行 时自动保存。 文件 2 是尚未通过主代码 运行 的新文件,但需要从中删除文件 1 的内容,以便主代码通过 运行 的数据较少。
错误似乎是“if not delete in finold:”没有按照我的预期执行。 我希望文件 2 中的字符串去查看文件 1 中是否存在相同的字符串。它似乎确实循环正确,但是它 returns 文件 2 中的所有字符串。我在下面给出了我的意思的示例.
我希望这是有道理的,我希望有人能给出答案。
finold 的样本是:
Srno,Flex Mood,Montana,08 Apr 2022 00:02
and for finnew it would be:
Bryan Mg,Bryan,Ça Sert à Rien,08 Apr 2022 00:11
Glowinthedark,Lituatie,Anders,08 Apr 2022 00:08
Architrackz,Perfecte Timing,Oh Baby (Whine),08 Apr 2022 00:05
Srno,Flex Mood,Montana,08 Apr 2022 00:02
that means that an example of fout would be:
Bryan Mg,Bryan,Ça Sert à Rien,08 Apr 2022 00:11
Glowinthedark,Lituatie,Anders,08 Apr 2022 00:08
您正在尝试迭代打开的文件缓冲区。如果你想遍历每一行,你需要阅读它们并将它们分成几行,就像你在第一个代码块中所做的那样 timelog
。
像这样:
# file 1
finold = open(path7 + time + 'temp.csv').read().split("\n")
# file 2
finnew = open(pathmain + "file2.csv").read().split("\n")
copyfinnew = open(path7 + "tempfile1.csv", 'w')
fout = open(path7 + "tempfile2.csv", 'w')
for line in finnew:
copyfinnew.write(line)
if line not in finold:
print(line, " not in finold")
fout.write(line + '\n')