Python |一致地重新格式化文本文件中的每行



我已经制作了自己的语料库拼错的单词。

misspellings_corpus.txt:

English, enlist->Enlish
Hallowe'en, Halloween->Hallowean

我的格式有问题。值得庆幸的是,它至少是一致的。

当前格式:

correct, wrong1, wrong2->wrong3
所需格式:

wrong1,wrong2,wrong3->correct
  • wrong<N>的顺序无关,
  • 每行可能有任意数量的wrong<N>单词(用逗号分隔:,),
  • 每行只有1个correct字(应该在->的右边)。

失败:

with open('misspellings_corpus.txt') as oldfile, open('new.txt', 'w') as newfile:
for line in oldfile:
correct = line.split(', ')[0].strip()
print(correct)
W = line.split(', ')[1].strip()
print(W)
wrong_1 = W.split('->')[0] # however, there might be loads of wrong words
wrong_2 = W.split('->')[1]
newfile.write(wrong_1 + ', ' + wrong_2 + '->' + correct)

输出new.txt(不工作):

enlist, Enlish->EnglishHalloween, Hallowean->Hallowe'en

解决方案:(灵感来自@alexis)

with open('misspellings_corpus.txt') as oldfile, open('new.txt', 'w') as newfile:
for line in oldfile:
#line = 'correct, wrong1, wrong2->wrong3'
line = line.strip()
terms = re.split(r", *|->", line)
newfile.write(",".join(terms[1:]) + "->" + terms[0] + 'n')

new.txt:

enlist,Enlish->English
Halloween,Hallowean->Hallowe'en

假设所有的逗号都是单词分隔符。为了方便,我将每行用逗号箭头分隔:

import re
line = 'correct, wrong1, wrong2->wrong3'
terms = re.split(r", *|->", line)
new_line = ", ".join(terms[1:]) + "->" + terms[0]
print(new_line)

你可以把它放回文件读取循环中,对吧?

我建议创建一个列表,而不是假设元素的数量。当你在逗号上分割时,第一个元素是正确的单词,元素[1:-1]是拼写错误,而[-1]将是你必须在箭头上分割的那个。

我想你也会发现write需要一个换行符,如"n"正如评论中建议的那样。

最新更新