Python Forum
Remove escape characters / Unicode characters from string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Remove escape characters / Unicode characters from string
#5
(May-15-2020, 08:59 AM)snippsat Wrote: Look like you are using text or content to get data back.
json do not look that like if website give back real json.
Example:
>>> import requests
>>> 
>>> r = requests.get('http://httpbin.org/get')
>>> r.json()
{'args': {},
 'headers': {'Accept': '*/*',
             'Accept-Encoding': 'gzip, deflate',
             'Host': 'httpbin.org',
             'User-Agent': 'python-requests/2.22.0',
             'X-Amzn-Trace-Id': 'Root=1-5ebe589e-a1eff28e599b122b81950144'},
 'origin': '46.246.118.243',
 'url': 'http://httpbin.org/get'}

# Now can acess data like this
>>> r.json()['origin']
'46.246.118.243'
Not json,here use text or content no will see \ or \\\ as as you get.
>> r.content
(b'{\n  "args": {}, \n  "headers": {\n    "Accept": "*/*", \n    "Accept-Encodi'
 b'ng": "gzip, deflate", \n    "Host": "httpbin.org", \n    "User-Agent": "py'
 b'thon-requests/2.22.0", \n    "X-Amzn-Trace-Id": "Root=1-5ebe589e-a1eff28e'
 b'599b122b81950144"\n  }, \n  "origin": "46.246.118.243", \n  "url": "http://'
 b'httpbin.org/get"\n}\n')

>>> r.text
('{\n'
 '  "args": {}, \n'
 '  "headers": {\n'
 '    "Accept": "*/*", \n'
 '    "Accept-Encoding": "gzip, deflate", \n'
 '    "Host": "httpbin.org", \n'
 '    "User-Agent": "python-requests/2.22.0", \n'
 '    "X-Amzn-Trace-Id": "Root=1-5ebe589e-a1eff28e599b122b81950144"\n'
 '  }, \n'
 '  "origin": "46.246.118.243", \n'
 '  "url": "http://httpbin.org/get"\n'
 '}\n')

The .json attribute would be very helpful but unfortunately, as I mentioned, the json data is stored inside a javascript variable so I have to return the data as a string to allow me to use .replace()
Reply


Messages In This Thread
RE: Remove escape characters / Unicode characters from string - by DreamingInsanity - May-15-2020, 12:55 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy xml content from webpage and save to locally without special characters Nik1811 14 807 Mar-26-2024, 09:28 AM
Last Post: Nik1811
  remove gilberishs from a "string" kucingkembar 2 256 Mar-15-2024, 08:51 AM
Last Post: kucingkembar
  sort search results by similarity of characters jacksfrustration 5 405 Feb-16-2024, 11:59 PM
Last Post: deanhystad
  non-latin characters in console from clipboard Johanson 3 688 Oct-26-2023, 10:10 PM
Last Post: deanhystad
Question Special Characters read-write Prisonfeed 1 605 Sep-17-2023, 08:26 PM
Last Post: Gribouillis
  doing string split with 2 or more split characters Skaperen 22 2,472 Aug-13-2023, 01:57 AM
Last Post: Skaperen
  How do I check if the first X characters of a string are numbers? FirstBornAlbratross 6 1,507 Apr-12-2023, 10:39 AM
Last Post: jefsummers
  use of escape character in re.sub and find WJSwan 1 904 Feb-16-2023, 05:19 PM
Last Post: Larz60+
  How to remove patterns of characters from text aaander 4 1,107 Nov-19-2022, 03:34 PM
Last Post: snippsat
Smile please help me remove error for string.strip() jamie_01 3 1,181 Oct-14-2022, 07:48 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020