简体   繁体   English

如何删除换行符并将所有单词添加到列表中

[英]How Can I Remove Newline and Add All Words To a List

I have a txt file it contains 4 lines.我有一个包含 4 行的 txt 文件。 (like a poem) The thing that I want is to add all words to one list. (就像一首诗)我想要的是将所有单词添加到一个列表中。 For example the poem like this:比如这首诗:

I am done with you,我和你结束了,

Don't love me anymore不再爱我

I want it like this: ['I', 'am', 'done', 'with', 'you', 'dont', 'love', 'me', 'anymore']我想要这样:['I', 'am', 'done', 'with', 'you', 'dont', 'love', 'me', 'anymore']

But I can not remove the row end of the first sentence it gives me 2 separated list.但我无法删除第一句的行尾,它给了我 2 个单独的列表。

romeo = open(r'd:\romeo.txt')
list = []

for line in romeo:
    line = line.rstrip()
    line = line.split()
    list = list + [line]
print(list)

You can use regular expresion like this.您可以像这样使用正则表达式

import re
poem = '' # your poem
split = re.split(r'\040|\n', poem)
print(split)

Regular expresion \040 is for white space an \n to match a new line.正则表达式\040用于空格和\n以匹配新行。

The output is: output 是:

['I', 'am', 'done', 'with', 'you,', "Don't", 'love', 'me', 'anymore']
with open(r'd:\romeo.txt', 'r') as msg:
    data = msg.read().replace("\n"," ")

data = [x for x in data.split() if x.strip()]

Even shorter:更短:

with open(r'd:\romeo.txt', 'r') as msg:
   list = " ".join(msg.split()).split(' ')

Or with removing the comma:或删除逗号:

with open(r'd:\romeo.txt', 'r') as msg:
   list = " ".join(msg.replace(',', ' ').split()).split(' ')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM