Bob
Bob

Reputation: 1396

Python: String on Single Line

This seems like a relatively simple thing to do, and i've tried a few solutions I've found on here but nothing seems to work. I am trying to remove all \ns from a string in order to have everything on one line. After researching, I thought

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

Would work, but I still can't get the string into one line. The issue here is when I try to turn the string into JSON it gives me errors as it's not valid JSON (using json.load as a test here).

Current Output:

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

Output I want:

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

Upvotes: 0

Views: 268

Answers (1)

Abstract
Abstract

Reputation: 995

You might still have \r (carriage returns) in the string.

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

Upvotes: 1

Related Questions