为什么在 python 中标记化时会得到多个列表?

Why do I get several lists when tokenizing in python?

我正在使用 Python 并从包含多个句子的文本文件中读取数据清理任务。对文本文件进行标记后,我不断得到一个列表,其中包含每个句子的标记,如下所示:

[u'does', u'anyone', u'think', u'that', u'we', u'have', u'forgotten', u'the', u'days', u'of', u'favours', u'for', u'the', u'pn', u's', u'party', u's', u'friends', u'of', u'friends', u'and', u'paymasters', u'some', u'of', u'us', u'have', u'longer', u'memories']

[u'but', u'is', u'the', u'value', u'at', u'which', u'vassallo', u'brothers', u'bought', u'this', u'property', u'actually', u'relevant', u'and', u'represents', u'the', u'actual', u'value', u'of', u'the', u'property']

[u'these', u'monsters', u'are', u'wrecking', u'the', u'reef', u'the', u'cargo', u'vessels', u'have', u'been', u'there', u'for', u'weeks', u'and', u'the', u'passenger', u'ship', u'for', u'at', u'least', u'24', u'hours', u'now', u'https', u'uploads', u'disquscdn', u'com'].

我正在做的代码如下:

with open(file_path) as fp:
    comments = fp.readlines()

    for i in range (0, len(comments)):

        tokens = tokenizer.tokenize(no_html.lower())
        print tokens

其中 no_html 是没有任何 html 标签的文本文件。有没有人可以告诉我如何将所有这些标记放入一个列表中?

不要使用 comments = fp.readlines(),请尝试使用 comments = fp.read()

readlines 的作用是读取文件的所有行,并returns将它们放入列表中。

您可以做的另一件事是,您可以将所有标记化的结果加入一个列表中。

all_tokens = []
for i in range (0, len(comments)):

        tokens = tokenizer.tokenize(no_html.lower())
        #print tokens
        all_tokens.extend(tokens)

print all_tokens