Python : 使用 in_place 模块根据另一个文本文件中的文本更新文本文件中的多个单词

Python : Updating multiple words in a text file based on text in another text file using in_place module

我有一个文本文件说 storyfile.txt
storyfile.txt 中的内容为

'Twas brillig, and the slithy toves Did gyre and gimble in the wabe; All mimsy were the borogoves, And the mome raths outgrabe


我有另一个文件 - hashfile.txt,其中包含一些用逗号 (,)
分隔的单词
hashfile.txt的内容是:

All,mimsy,were,the,borogoves,raths,outgrabe


我的objective

我的 objective 是
1.阅读hashfile.txt
2.在每个逗号分隔的单词上插入标签
3. 阅读 storyfile.txt 。搜索与 hashtag.txt 中相同的词并在这些词上添加主题标签。
4. 用哈希标记的词更新 storyfile.txt

到目前为止我的 Python 代码

import in_place

hashfile = open('hashfile.txt', 'w+')
n1 = hashfile.read().rstrip('\n')
print(n1)

checkWords = n1.split(',')
print(checkWords)

repWords = ["#"+i for i in checkWords]
print(repWords)
hashfile.close()

with in_place.InPlace('storyfile.txt') as file:
    for line in file:
        for check, rep in zip(checkWords, repWords):
            line = line.replace(check, rep)
            file.write(line)

输出

这里可以看到 https://dpaste.de/Yp35

为什么会出现这种输出? 为什么最后一句没有换行符? 我哪里错了?
The output
附图

单个文本的当前工作代码

import in_place

with in_place.InPlace('somefile.txt') as file:
    for line in file:
        line = line.replace('mome', 'testZ')
        file.write(line)

看看这是否有帮助。这满足了你提到的 objective,虽然我没有使用 in_place 模块。

hash_list = []
with open("hashfile.txt", 'r') as f:
    for i in f.readlines():
        for j in i.split(","):
            hash_list.append(j.strip())
with open("storyfile.txt", "r") as f:
    for i in f.readlines():
        for j in hash_list:
            i = i.replace(j, "#"+j)
        print(i)

如果您需要进一步说明,请告诉我。