escaping

In Python, is it possible to escape newline characters when printing a string?

In Python, is it possible to escape newline characters when printing a string? Question: I want the newline n to show up explicitly when printing a string retrieved from elsewhere. So if the string is ‘abcndef’ I don’t want this to happen: >>> print(line) abc def but instead this: >>> print(line) abcndef Is there a …

Total answers: 3

How do I .decode('string-escape') in Python 3?

How do I .decode('string-escape') in Python 3? Question: I have some escaped strings that need to be unescaped. I’d like to do this in Python. For example, in Python 2.7 I can do this: >>> "\123omething special".decode(‘string-escape’) ‘Something special’ >>> How do I do it in Python 3? This doesn’t work: >>> b"\123omething special".decode(‘string-escape’) Traceback …

Total answers: 6

How can I remove the ANSI escape sequences from a string in python

How can I remove the ANSI escape sequences from a string in python Question: Here is a snippet that includes my string. ‘lsrnx1b[00mx1b[01;31mexamplefile.zipx1b[00mrnx1b[01;31m’ The string was returned from an SSH command that I executed. I can’t use the string in its current state because it contains ANSI standardized escape sequences. How can I programmatically remove …

Total answers: 8

BeautifulSoup is HTML escaping strings which have escaped characters

BeautifulSoup is HTML escaping strings which have escaped characters Question: I am reading a string from file: a = ‘<script>closedSign: ‘<img src=”/static/images/drop-down.png” style=”margin-top: -3px;” />'</script>’ Now, when I run BeautifulSoup(a) <script>closedSign: ‘&lt;img src=”/static/images/drop-down.png” style=”margin-top: -3px;” /&gt;'</script> Thus, <img is being HTML escaped into &lt;img How can I avoid this? Asked By: Jamal || Source Answers: …

Total answers: 3

How can I selectively escape percent (%) in Python strings?

How can I selectively escape percent (%) in Python strings? Question: I have the following code test = “have it break.” selectiveEscape = “Print percent % in sentence and not %s” % test print(selectiveEscape) I would like to get the output: Print percent % in sentence and not have it break. What actually happens: selectiveEscape …

Total answers: 6

Valid JSON giving JSONDecodeError: Expecting , delimiter

Valid JSON giving JSONDecodeError: Expecting , delimiter Question: I’m trying to parse a json response data from youtube api but i keep getting an error. Here is the snippet where it choking: data = json.loads(“””{ “entry”:{ “etag”:”W/”A0UGRK47eCp7I9B9WiRrYU0.”” } }”””) ..and this happens: JSONDecodeError: Expecting , delimiter: line 1 column 23 (char 23) I’ve confirmed that …

Total answers: 2

Decode escaped characters in URL

Decode escaped characters in URL Question: I have a list containing URLs with escaped characters in them. Those characters have been set by urllib2.urlopen when it recovers the html page: http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=edit http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&action=history http://www.sample1webpage.com/index.php?title=%E9%A6%96%E9%A1%B5&variant=zh Is there a way to transform them back to their unescaped form in python? P.S.: The URLs are encoded in utf-8 Asked …

Total answers: 5

How to use a variable inside a regular expression?

How to use a variable inside a regular expression Question: I’d like to use a variable inside a regex, how can I do this in Python? TEXTO = sys.argv[1] if re.search(r”b(?=w)TEXTOb(?!w)”, subject, re.IGNORECASE): # Successful match else: # Match attempt failed Asked By: Pedro Lobito || Source Answers: rx = r’b(?<=w){0}b(?!w)’.format(TEXTO) Answered By: Cat Plus …

Total answers: 12

Escaping quotes in string

Escaping quotes in string Question: I have a python dictionary e.g.: [{“pk”:”1″,”name”:”John”,”size”:”1/4″ “},{},{},etc] That size is 1/4 inch,how would I “escape” that quote? So it still would display it as 1/4″, Its a list of things, so I cant just manually code it like 1/4″, I tried replace(‘”‘,'”‘) EDIT: The orginal list is a textfield …

Total answers: 5

How to make Python use a path that contains colons in it?

How to make Python use a path that contains colons in it? Question: I have a program that includes an embedded Python 2.6 interpreter. When I invoke the interpreter, I call PySys_SetPath() to set the interpreter’s import-path to the subdirectories installed next to my executable that contain my Python script files… like this: PySys_SetPath(“/path/to/my/program/scripts/type1:/path/to/my/program/scripts/type2”); (except …

Total answers: 2