Python字符串替换不工作的新行

use*_*126 5 python string replace

我有一个关键字文件,我只想用逗号替换新行

print file("keywords.txt", "r").read().replace("\n", ", ")
Run Code Online (Sandbox Code Playgroud)

尝试了所有的变化 \r\n

Di *_*Zou 0

我刚刚尝试过这个,它对我有用。你的文件是什么样的?

我的“temp.txt”文件如下所示:

a
b
c
d
e

我的 python 文件有这样的代码:

print file("temp.txt", "r").read().replace("\n", ", ")
Run Code Online (Sandbox Code Playgroud)

这是我的输出:

>python temp_read.py
a、b、c、d、e、