Python 从文件的每一行中删除enter

Python 从文件的每一行中删除enter,python,text,operating-system,Python,Text,Operating System,我是Python新手。是否有办法将文件内容更改为我想要的方式? 这是我的txt文件 work money apple dog 我想把这个改成 work money apple dog 我不知道如何把它们放在一行,其余的我都明白了 我现在的代码 fn = 'settings.txt' f = open(fn) for line in f: f.join(f.read().splitlines()) 可以使用str strip方法删除行尾的空格和换行符 所以,大概是这样的: words =

我是Python新手。是否有办法将文件内容更改为我想要的方式? 这是我的txt文件

work
money
apple
dog
我想把这个改成

work money apple dog
我不知道如何把它们放在一行,其余的我都明白了 我现在的代码

fn = 'settings.txt'
f = open(fn)
for line in f:
  f.join(f.read().splitlines())
可以使用str strip方法删除行尾的空格和换行符

所以,大概是这样的:

words = list()

with open("my_file_path") as file:
    for line in file:
        words.append(line.strip())

result = " ".join(words)
使用生成器表达式:

with open("my_file_path") as file:
    result = " ".join(line.strip() for line in file)
这项工作:

将打开的“文件路径”设置为f_1: res_str=''.joinf_1.read.splitlines
你被困在哪里?您可以逐行读取项目,并将它们连接起来。使用+或join。写下结果字符串。我已经打开了文件,现在我停留在F中的for行,这是打开的文件欢迎使用stackoverflow!请按照@Prune的建议提交一些您尝试的代码。你可以看看好的,我已经编辑了我的帖子,但是@AlexanderCécile解决方案更好:@Eraw awn