如何将换行器放在txt文件中的特定位置?

问题描述

我有一个.txt文件,其中包含很多研究所需的文本信息。因此,我正在尝试编写一个进行关键字搜索的程序(在我的情况下,我需要短语“ sold salt”),然后将以该短语开头的文本逐行写入新文件并剪切在某个时候关闭(我还没有决定)。这实际上是一本书,其中包含17世纪的数字化文档,用旧俄文写成,但是示意性的文本看起来像是:

“sheet_№1

文字文字文字文字

文字文字

文本文本文本文本文本文本出售盐文本文本文本文本出售盐文本文本文本文本文本文本

文字文字文字文字

sheet_№1_reverse

文本文本卖盐文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本文本“

所以这是一个非常糟糕的结构化事情,我想要的是将所有盐销售记录及其在全文中的位置放在一个文件中,以供我研究。

现在,对不起,很长的介绍,我只是想展示我需要处理的内容。

我尝试使用docx lib编写代码,但事实证明,唯一可行的方法是在docx文件中下划线所需的信息,然后通过使用代码将其删除,这并不是很糟糕,但仍然需要时间。

所以我停止了txt格式,现在我明白了:

key_1 = 'sold'
key_2 = 'salt'

f_old = open("text.txt",encoding='utf-8')
f_result = open("text_result.txt",'w',encoding='utf-8')

for line in f_old:
    line = line.split()
    if len(line) == 1:
        for elem in range(len(line)):
            f_result.write(line[elem] + '\n')
    else:
        if key_1 in line and key_2 in line:
            for word in range(len(line)):
                if line[word] == key_1 and line[word + 1] == key_2:
                    for elem in line[word: word + 10]:
                        f_result.write(elem + ' ')
                    f_result.write('\n')

f_old.close()
f_result.close()

基于上面的示例,它给了我这个结果:

“sheet_№1

卖盐文本文本文本文本文本 卖盐 文本

卖盐 文本文本文本文本文本文本

sheet_№1_reverse

卖盐文本文本文本文本文本文本文本文本文本

用我的双手剪掉“卖盐”和其他多余的信息(例如在第二行的末尾)并不是什么大问题,因为无论如何,我将使用包含比我需要的更多信息的行来执行此操作。但是,如果有什么想法可以让我的关键字在该行中出现两次或更多次,该如何减少行数?

我有一个想法,不仅要打开text_result进行写作,而且还要阅读,然后再通过以下方式剪掉这些行:

for line in f_result:
    line = line.split()
    if len(line) > 1:
        for word in line[::-1]:
            while line[word] != key_1:
                line.pop([word])

但是如果我将其放在这样的代码中,它将不起作用:

key_1 = 'sold'
key_2 = 'salt'
f_old = open("text.txt",'w+',encoding='utf-8')

for line in f_old:
    line = line.split()
    if len(line) == 1:
        for elem in range(len(line)):
            f_result.write(line[elem] + '\n')
    else:
        if key_1 in line and key_2 in line:
            for word in range(len(line)):
                if line[word] == key_1 and line[word + 1] == key_2:
                    for elem in line[word: word + 7]:
                        f_result.write(elem + ' ')
                    f_result.write('\n')

for line in f_result:
    line = line.split()
    if len(line) > 1:
        for word in line[::-1]:
            while line[word] != key_1:
                line.pop([word])

f_old.close()
f_result.close()

我只是想念一些基本的东西吗?

提前谢谢!

解决方法

因此,根据您提供的信息,我想您要在看到另一个sold salt后停止写,然后从那里继续写。这意味着在编写时,您只需进行另一项检查(就像您已经做过的一样),检查将要进入新文件的单词不是sold salt,如果是,则从那里跳出来。看起来像这样:

for line in f_old:
    line_words = line.split()  # it is confusing changing the value of a variable within the
    # loop,so I would recommend simply creating a new variable
    if len(line_words) == 1:
        # there was no need for a for loop here as we already know that there is only one element
        f_result.write(line_words[0] + '\n')
    else:
        for word in range(len(line_words)-1):  # as you will be accessing word+1 element,# you need to look out for out of range indices
            if line_words[word] == key_1 and line_words[word + 1] == key_2:
                for i in range(len(line_words[word: word + 10]))):
                    if i != 0 and line_words[word+i] == key_1 and line_words[word+i+1] == key_2:
                        break

                    f_result.write(line_words[word+i] + ' ')
                f_result.write('\n')


f_result.close()

我还建议使用enumerate,然后仅使用索引来访问所需元素的元素,我认为它提供了更简洁的代码。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...