要从字符串列创建列表

时间:2019-08-02 04:43:51

标签: python-3.x split strip

我将一个字符串段落作为输入,然后使用rstrip()将其剥离为行,然后使用split()将每一行剥离为单词。

所以我剩下一串字符串:

巴里 莎莉 安德鲁 乔纳森 ...

如何从此输出创建列表?

(我希望将此作为输出)=> ['Barry', 'Sally', 'Andrew', 'Jonathan', ...]

file_name = input("Enter file name: ")
file_holder = open(file_name)

对于file_holder中的行:

 # strip into rows
    line = line.rstrip()
    if not line.startswith("From "): continue
    # strip into words
    words = line.split()
    email_list = words[1]
    print(email_list)

谢谢

1 个答案:

答案 0 :(得分:1)

在您的代码中,

...
words = line.split()
print(words)