Removing continuation characters in the middle of a string in Python

13,223

Solution 1

results_str.replace('\n', "") 

You're so close. The \ is an escape character in string literals, so you must either double it up, or use a raw string:

results_str.replace('\\n', "") 
results_str.replace(r'\n', "") 

Also remember that Python strings are immutable: replace() returns a new string, because the existing string cannot be modified. If you want to keep the changed string, you have to assign it to some name, such as the original name:

results_str = results_str.replace(r'\n', "")

Although you may want to process your data in a fashion that doesn't care about the newlines. That looks like JSON, so rather than trying to parse it yourself you should probably be using the Python json module.

Solution 2

This is a JSON response. Instead of trying to mess with the string, you should use the json module to parse the string:

import json
result = b'{\n   "destination_addresses" : ...'
obj = json.loads(obj.decode('utf-8'))
Share:
13,223
Admin
Author by

Admin

Updated on August 02, 2022

Comments

  • Admin
    Admin 10 months

    The standard return value from the Google maps API looks like this - after conversion from bytes to a string:

    b'{\n   "destination_addresses" : [ "Washington, DC, USA" ],\n   "origin_addresses" : [ "New York, NY, USA" ],\n   "rows" : [\n      {\n         "elements" : [\n            {\n               "distance" : {\n                  "text" : "370 km",\n                  "value" : 369720\n               },\n               "duration" : {\n                  "text" : "3 hours 48 mins",\n                  "value" : 13672\n               },\n               "status" : "OK"\n            }\n         ]\n      }\n   ],\n   "status" : "OK"\n}\n'
    

    In order to process is any further, I would like to remove all the line break characters first. However, I don't know how to do it

    results_str.replace('\n', "") 
    

    won't work, .i.e., it returns the same string without replacing the '\n's, because of the line continuation character.

    Same thing happens when I double the backslash

    results_str.replace('\\n', "") 
    

    Any tips?