首页 > 解决方案 > 将文件中每一行的第三个单词分配给 Python 中的列表

问题描述

我有一个文本文件'news.txt',我正在尝试将每一行的第三个单词保存在一个列表中(三个),我只是一遍又一遍地得到一个带有第三个单词的向量,它似乎没有注册以下几行中的其他词。

with open('news.txt', 'r') as file:
    content = file.read()
    words = content.split() 
    for bwords in words:
        three = bwords[2]

标签: pythonlistsplit

解决方案


with open('news.txt', 'r') as file:
    content = file.read()
    words = content.split()
    for bwords in words[::3]:
        three = bwords

推荐阅读