Python字符串替换不起作用的新行

发布于 2024-12-11 06:20:55 字数 149 浏览 0 评论 0原文

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

print file("keywords.txt", "r").read().replace("\n", ", ")

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

I have a keywords file I just wanted to replace the new lines with commas

print file("keywords.txt", "r").read().replace("\n", ", ")

tried all variations of \r\n

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

久夏青 2024-12-18 06:20:55

别忘了,这是Python!始终寻找简单的方法...

', '.join(mystring.splitlines())

Don't forget, this is Python! Always look for the easy way...

', '.join(mystring.splitlines())

两仪 2024-12-18 06:20:55

您的代码应该按编写的方式工作。然而,这里还有另一种方法。

让 Python 为您分割行(for line in f)。使用open而不是file。使用 with 这样您就不需要手动关闭文件。

with open("keywords.txt", "r") as f:
    print ', '.join(line.rstrip() for line in f) # don't have to know line ending!

Your code should work as written. However, here's another way.

Let Python split the lines for you (for line in f). Use open instead of file. Use with so you don't need to close the file manually.

with open("keywords.txt", "r") as f:
    print ', '.join(line.rstrip() for line in f) # don't have to know line ending!
许久 2024-12-18 06:20:55

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

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

一个
b
c
d

和我的 python 文件有以下代码:

print file("temp.txt", "r").read().replace("\n", ", ")

这是我的输出:

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

I just tried this and it works for me. What does your file look like?

My "temp.txt" file looked like this:

a
b
c
d
e

and my python file had this code:

print file("temp.txt", "r").read().replace("\n", ", ")

This was my output:

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

流年里的时光 2024-12-18 06:20:55

您编写的代码在我创建的测试文件中适用于我。

您是否正在尝试将结果写回到文件中?

您可以尝试在十六进制编辑器中查看输入文件以查看行结尾。

The code you wrote works for me in a test file I created.

Are you trying to write the results back to a file?

You could try looking at the input file in a hex editor to see the line endings.

心凉 2024-12-18 06:20:55

您想替换实际的文件内容吗?像这样:

newContent = file("keywords.txt", "r").read().replace("\r", "").replace("\n", ", ")
open("keywords.txt", "w").write(newContent)

Did you want to replace the actual file contents? Like this:

newContent = file("keywords.txt", "r").read().replace("\r", "").replace("\n", ", ")
open("keywords.txt", "w").write(newContent)
辞旧 2024-12-18 06:20:55

我刚刚遇到了同样的问题,这是解决方案:

phones = arr[8].replace("\\n", "|")

I've just had the same problem and this was the solution to it:

phones = arr[8].replace("\\n", "|")
九局 2024-12-18 06:20:55

我也遇到了这个问题并用以下方法解决:

new_text = ",".join(text.split("\n"))

我的“\n”在我的单元测试(以 str 作为输入)和我的集成测试(以文件读取导入作为输入)之间没有以相同的方式解释,所以 str.replace ("\n", ",") 不适用于这两个测试。

编辑=>这两种方法都有效:

text = text.replace("\\n", ",")
text = text.replace("\n", ",")

我不知道为什么 file 和 str 输入之间存在不同...

I had this problem too and solve it with :

new_text = ",".join(text.split("\n"))

My "\n" wasn't interpreted in the same way between my unit test (with str as input) and my integration test (with file reading import as input), so str.replace("\n", ",") didn't work with both tests.

EDIT => With both this works too :

text = text.replace("\\n", ",")
text = text.replace("\n", ",")

I don't know why is it different between file and str input...

碍人泪离人颜 2024-12-18 06:20:55

使用双斜杠 n 而不是 \n 进行搜索。这花费了我一生的时间。当我尝试粘贴它时,该网站甚至将其更改为 \n 。不该说的组合。 “”+“”+“n”将是尝试解释它的字符串组合。

Search on double slash n instead of \n. That cost hours of my life. This website even changes it to \n when I tried to paste it. The combo that shall not be spoken. ""+""+"n" would be the string combo to try to explain it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文