-1

I am writing a program that gets some data from an API, the response is simple text (no JSON or XML), since the data is in plain text, it also contains escape sequences like \n and \r

enter image description here

Here's the data as printed by postman. When I try to split the lines using .split(":"), the escape sequences get in the way.

Here's the code:

def getSuffix(password):
     api_resp = get(f"https://api.pwnedpasswords.com/range/{prefix}")
     api_resp = api_resp.text
     print(api_resp.split(":"))

and here's what I get: enter image description here

I've tried using .strip("\r\n") as well, but it does not seem to work.

Here's the desired output: enter image description here

I want the string and the number associated be in a list and this list to be a part of another list.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
  • What exactly do you want to get as a result instead? Please show a smaller example as actual text, not as images. – mkrieger1 Aug 18 '21 at 07:11
  • If you want to split the lines, why do you not use the `splitlines` string method? – mkrieger1 Aug 18 '21 at 07:12
  • @mkrieger1 I was not aware that such a method existed, this seems to solve my problem, thanks a ton – Himanshu Sardana Aug 18 '21 at 07:15
  • Does this answer your question? [How do I split a multi-line string into multiple lines?](https://stackoverflow.com/questions/172439/how-do-i-split-a-multi-line-string-into-multiple-lines) – mkrieger1 Aug 18 '21 at 07:19

2 Answers2

1

You are trying to apply all the operations over the complete text instead of lines. The reason .strip("\r\n") did not work is because it strips the characters from leading and trailing places of the complete text.You can use .replace("\r\n","") for the removal and then process the text using .split(":") for every line as mentioned by @mkrieger1

Omkar
  • 791
  • 1
  • 9
  • 26
0

Thanks to @mkrieger1 for suggesting the .splitlines() method. It seems to fix my issue.