首页 > 解决方案 > 从字符串列表中提取第一个选项卡之前的所有文本

问题描述

所以我有这个来自http://www.manythings.org/anki/的 text_data 它看起来像这样

['Hi.\tHallo!\tCC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #380701 (cburgmer)\n',
 'Hi.\tGrüß Gott!\tCC-BY 2.0 (France) Attribution: tatoeba.org #538123 (CM) & #659813 (Esperantostern)\n',
 'Run!\tLauf!\tCC-BY 2.0 (France) Attribution: tatoeba.org #906328 (papabear) & #941078 (Fingerhut)\n',
 'Wow!\tPotzdonner!\tCC-BY 2.0 (France) Attribution: tatoeba.org #52027 (Zifre) & #2122382 (Pfirsichbaeumchen)\n',
 'Wow!\tDonnerwetter!\tCC-BY 2.0 (France) Attribution: tatoeba.org #52027 (Zifre) & #2122391 (Pfirsichbaeumchen)\n',
 'Fire!\tFeuer!\tCC-BY 2.0 (France) Attribution: tatoeba.org #1829639 (Spamster) & #1958697 (Tamy)\n',
 'Help!\tHilfe!\tCC-BY 2.0 (France) Attribution: tatoeba.org #435084 (lukaszpp) & #575889 (MUIRIEL)\n',
...
]

我做了这个

English = []
for sent in data_examples:
    pattern  = re.compile(r'.+?\t')
    matches = pattern.finditer(sent)
    for match in matches:
        English.append(match)

如何捕捉文本中的英语?我的不是真的工作。

标签: pythonstringlistsplit

解决方案


您的英语段位于第一列。

你需要做的就是

English = [sent.split('\t')[0] for sent in data_examples]

推荐阅读