5

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?

  • What do you mean with "won't work"? If I do `str.replace("\n", "")`, it returns the same string without all the `\n`s – jazzpi Apr 14 '13 at 16:27
  • Based on how the matter was resolved, the problem isn't anything to do with string escaping, but was simply failing to understand that `.replace` *creates a new string*. Closed as an appropriate duplicate. (Recommend deletion, because this makes very little sense as a signpost.) – Karl Knechtel Aug 06 '22 at 01:15

2 Answers2

15
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.

kindall
  • 178,883
  • 35
  • 278
  • 309
  • Thanks for the comment. I tried the doubling before: `results_str.replace('\\n', "")`, but for some reason it doesn't work. I will take a look at the JSON module –  Apr 14 '13 at 16:30
  • Wait, are you not reassigning the result back to `results_str`? I'll add some stuff about that. – kindall Apr 14 '13 at 16:31
  • @bluewoodtree: Do you do `results_str = results_str.replace('\\n', "")`? – jazzpi Apr 14 '13 at 16:33
  • omg. I am sorry, it's too early in the morning...embarrassing...vote me down and close this question... But thanks for the JSON recommendations, it looks very convenient –  Apr 14 '13 at 16:35
1

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'))
mata
  • 67,110
  • 10
  • 163
  • 162