escaping

ignoring backslash character in python

ignoring backslash character in python Question: If I have: a = "fwd" b = "fwd" how can I ignore the "" so something like print(a in b) can evaluate to True? Asked By: Kevin R. || Source Answers: One way of doing it using raw strings: >>> a = “fwd” >>> b = “fwd” >>> …

Total answers: 4

Python: Replacing backslashes to avoid escape sequences in string

Python: Replacing backslashes to avoid escape sequences in string Question: I´m trying to replace the single backslashes i get within a string with double backslashes, because sometimes the the “backslash+character” combination creates an escape sequence. I have tried various ways (mostly from other stackoverflow questions), but nothing gets me the correct results so far. Example …

Total answers: 1

Why does printing a tuple in Python double the backslashes?

Why does printing a tuple (list, dict, etc.) in Python double the backslashes? Question: In Python, when I print a string with a backslash, it prints the backslash only once: >>> print(r’C:hi’) C:hi >>> print(‘C:\hi’) C:hi But I noticed that when you print a tuple of strings with backslashes, it prints a double backslash: >>> …

Total answers: 1

How does v differ from x0b or x0c?

How does v differ from x0b or x0c? Question: Typing string.whitespace gives you a string containing all whitespace characters defined by Python’s string module: ‘tnx0bx0cr ‘ Both x0b and x0c seem to give a vertical tab. >>> print ‘firstx0bsecond’ first second v gives the same effect. How are these three different? Why does the string …

Total answers: 1

Why do backslashes appear twice?

Why do backslashes appear twice? Question: When I create a string containing backslashes, they get duplicated: >>> my_string = "whydoesithappen?" >>> my_string ‘why\does\it\happen?’ Why? Asked By: Zero Piraeus || Source Answers: What you are seeing is the representation of my_string created by its __repr__() method. If you print it, you can see that you’ve actually …

Total answers: 2

How to print a single backslash?

How can I print a single backslash? Question: When I write print(”) or print("") or print("”"), Python doesn’t print the backslash symbol. Instead it errors for the first two and prints ” for the third. What should I do to print a backslash? This question is about producing a string that has a single backslash …

Total answers: 4

How to escape special characters of a string with single backslashes

How to escape special characters of a string with single backslashes Question: I’m trying to escape the characters -]^$*. each with a single backslash . For example the string: ^stack.*/overflow$arr=1 will become: ^stack.*/overflo\w$arr=1 What’s the most efficient way to do that in Python? re.escape double escapes which isn’t what I want: ‘\^stack\.\*\/overflow\$arr\=1’ I need this …

Total answers: 6

How do I automatically fix an invalid JSON string?

How do I automatically fix an invalid JSON string? Question: From the 2gis API I got the following JSON string. { “api_version”: “1.3”, “response_code”: “200”, “id”: “3237490513229753”, “lon”: “38.969916127827”, “lat”: “45.069889625267”, “page_url”: null, “name”: “ATB”, “firm_group”: { “id”: “3237499103085728”, “count”: “1” }, “city_name”: “Krasnodar”, “city_id”: “3237585002430511”, “address”: “Turgeneva, 172/1”, “create_time”: “2008-07-22 10:02:04 07”, “modification_time”: “2013-08-09 …

Total answers: 9

Saving utf-8 texts with json.dumps as UTF8, not as u escape sequence

Saving UTF-8 texts with json.dumps as UTF-8, not as a u escape sequence Question: Sample code (in a REPL): import json json_string = json.dumps("ברי צקלה") print(json_string) Output: "u05d1u05e8u05d9 u05e6u05e7u05dcu05d4" The problem: it’s not human readable. My (smart) users want to verify or even edit text files with JSON dumps (and I’d rather not use XML). …

Total answers: 12

Escape (Insert backslash) before brackets in a Python string

Escape (Insert backslash) before brackets in a Python string Question: I need to format many strings that contain a similar structure: u’LastName FirstName (Department / Subdepartment)’ My wish is to get the string to look like this: u’LastName FirstName (Department / Subdepartment)’ Meaning I need to add a backslash to the opening bracket and to …

Total answers: 2