分词,保持位置

2024-04-25 00:41:39 发布

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

我有一个文本文件,例如:

<1.0>The man went up the hill.
<1.1>The woman sat on the bench.

有没有办法把每个词分开,保持它的位置。因此它将输出:

<1.0>The
<1.0>man
...
<1.1>The
<1.1>woman

我见过许多str.split(" ");的例子,但是没有找到一种方法来保持每行的第一个单词与被拆分的单词在一起。即使我不能保留括号<>,数字也很重要。你知道吗

如果JavaScript中没有函数,那么Python中有函数吗?你知道吗


Tags: the函数on单词sat文本文件upbench
1条回答
网友
1楼 · 发布于 2024-04-25 00:41:39

有件事让你开始。去吧,试试看。我用了一系列分开的电话来获取所需的信息。你知道吗

text = """<1.0>The man went up the hill.
<1.1>The woman sat on the bench."""

for line in text.split("\n"):
    parts = line.split(">")
    number = parts[0] + ">"
    for word in parts[1].split(" "):
        print number + word

相关问题 更多 >