Python:单行字符串

Python: String on Single Line

这似乎是一件相对简单的事情,我已经尝试了一些我在此处找到的解决方案,但似乎没有任何效果。我试图从一个字符串中删除所有 \ns 以便将所有内容都放在一行中。经过研究,我认为

jsfile = jsfile.replace("\n", " ")
jsfile = jsfile.replace("\n\n", " ")
jsfile = jsfile.replace("\t", " ")

可以,但我仍然无法将字符串排成一行。这里的问题是,当我尝试将字符串转换为 JSON 时,它给了我错误,因为它无效 JSON(此处使用 json.load 作为测试)。

当前输出:

{"name": "aName", "description": "a description that
doesn't want to 
stay on one line", "address": "anAddress"}

我想要的输出:

{"name": "aName", "description": "a description that doesn't want to stay on one line", "address": "anAddress"}

字符串中可能还有 \r(回车符 returns)。

jsfile = jsfile.replace('\n', ' ').replace('\t', ' ').replace('\r', ' ')