【问题标题】:How to put a line breaker inside a specific position in the txt file?如何在 txt 文件中的特定位置放置换行符?
【发布时间】:2021-02-07 23:47:02
【问题描述】:

我有一个 .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

sold salt文字文字文字文字文字文字sold salt文字

卖盐文字文字文字文字文字文字

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", 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 + 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()

我只是缺少一些基本的东西吗?

提前致谢!!!

【问题讨论】:

  • 您希望实际结果如何?
  • @Marko,是的,你有我的想法。感谢您的关注和有用的回答!我也很高兴您建议我尝试“枚举”,因为我是 Python 和编程的初学者! =)

标签: python python-3.x line cut txt


【解决方案1】:

所以根据您提供的信息,我想您想在看到另一个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,然后只使用索引来访问您需要的元素后面的元素,我认为它提供了更简洁的代码。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-10
    • 1970-01-01
    • 2018-01-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多