Python如何将连字符的单词与换行符合并?

2024-09-29 01:26:23 发布

您现在位置:Python中文网/ 问答频道 /正文

I want to say that Napp Granade
serves in the spirit of a town in our dis-
trict of Georgia called Andersonville.

我有数以千计的文本文件,上面的数据和文字已经包装使用连字符和换行。在

我要做的是删除连字符并将换行符放在单词的末尾。我不想删除所有连字符的单词,如果可能的话,只删除行尾的单词。在

^{pr2}$

上面的代码不工作,我尝试了几种不同的方法。在

我想浏览整个文本文件并删除所有表示换行符的连字符。例如:

I want to say that Napp Granade
serves in the spirit of a town in our district
of Georgia called Andersonville.

任何帮助都将不胜感激。在


Tags: ofthetointhatour字符单词
1条回答
网友
1楼 · 发布于 2024-09-29 01:26:23

您不需要使用正则表达式:

filename = 'test.txt'

# I want to say that Napp Granade
# serves in the spirit of a town in our dis-
# trict of Georgia called Anderson-
# ville.

with open(filename, encoding="utf8") as f:
    lines = [line.strip('\n') for line in f]
    for num, line in enumerate(lines):
        if line.endswith('-'):
            # the end of the word is at the start of next line
            end = lines[num+1].split()[0]
            # we remove the - and append the end of the word
            lines[num] = line[:-1] + end
            # and remove the end of the word and possibly the 
            # following space from the next line
            lines[num+1] = lines[num+1][len(end)+1:]

    text = '\n'.join(lines)

with open(filename, "w", encoding="utf8") as f:
    f.write(text)


# I want to say that Napp Granade
# serves in the spirit of a town in our district
# of Georgia called Andersonville.

但你可以,当然,而且时间更短:

^{pr2}$

我们寻找-后跟\n,并捕获下面的单词,这是拆分单词的结尾。
我们将所有这些替换为捕获的单词后跟一个新行。在

为了正确解释\1,不要忘记使用原始字符串进行替换。在

相关问题 更多 >