0

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"}
Bob
  • 1,344
  • 3
  • 29
  • 63
  • Looks like it ought to work unless maybe there is an alternative kind of newline, for example if jsfile came from a windows file: https://stackoverflow.com/questions/4599936/handling-r-n-vs-n-newlines-in-python-on-mac-vs-windows – Andrew Allaire Jul 21 '21 at 18:35
  • What does your input look like exactly? How are you producing your output? Please include a MWE to demonstrate the problem you're having. – Woodford Jul 21 '21 at 18:35

1 Answers1

1

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

jsfile = jsfile.replace('\n', ' ').replace('\t', ' ').replace('\r', ' ')
Abstract
  • 985
  • 2
  • 8
  • 18