escaping

Convering double backslash to single backslash in Python 3

烂漫一生 提交于 2019-11-30 22:51:50
I have a string like so: >>> t '\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0' That I made using a function that converts unicode to the representative Python escape sequences. Then, when I want to convert it back, I can't get rid of the double backslash so that it is interpreted as unicode again. How can this be done? >>> t = unicode_encode(" >>> t '\\u0048\\u0065\\u006c\\u006c\\u006f\\u0020\\u20ac\\u0020\\u00b0' >>> print(t) \u0048\u0065\u006c\u006c\u006f\u0020\u20ac\u0020\u00b0 >>> t.replace('\\','X') 'Xu0048Xu0065Xu006cXu006cXu006fXu0020Xu20acXu0020Xu00b0' >>> t.replace('

Replace “&” to “\\&” in Ruby seems impossible?

落爺英雄遲暮 提交于 2019-11-30 22:45:56
I want to replace all & characters into \& with String.gsub (or a other method). I've tried several combinations and read another question here , but nothing is gonna work. "asdf & asdf".gsub("&", "\\\&") => "asdf & asdf" Your linked question provides a solution - use the block form of gsub : irb(main):009:0> puts "asdf & asdf".gsub("&"){'\&'} asdf \& asdf I'm going to guess that you're using 1.8. In 1.8, irb says this: >> "asdf & asdf".gsub("&", "\\\&") => "asdf & asdf" >> puts "asdf & asdf".gsub("&", "\\\&") asdf & asdf And that matches what you're seeing. But, if you add yet another

Javascript unescape() vs. Python urllib.unquote()

假如想象 提交于 2019-11-30 22:28:07
From reading various posts, it seems like JavaScript's unescape() is equivalent to Pythons urllib.unquote() , however when I test both I get different results: In browser console: unescape('%u003c%u0062%u0072%u003e'); output: <br> In Python interpreter: import urllib urllib.unquote('%u003c%u0062%u0072%u003e') output: %u003c%u0062%u0072%u003e I would expect Python to also return <br> . Any ideas as to what I'm missing here? Thanks! %uxxxx is a non standard URL encoding scheme that is not supported by urllib.parse.unquote() (Py 3) / urllib.unquote() (Py 2). It was only ever part of ECMAScript

filter_var vs htmlentities vs htmlspecialchars

ぃ、小莉子 提交于 2019-11-30 22:15:01
问题 Disclaimer This is not a question about whether we should be escaping for database input. This is strictly looking at the technical differences between the three functions in the title. There is this question discussing the difference between htmlentities() and htmlspecialchars(). But, it doesn't really discuss filter_var() and the information I found on Google was more along the lines of "Make sure you escape user input before it is echo'd!" My questions are: Why are htmlspecialchars() and

escape a string for shell commands in Python [duplicate]

主宰稳场 提交于 2019-11-30 22:05:22
This question already has an answer here: How to escape os.system() calls? 10 answers I'm interested to escape a string in Python3.x, such as: SOME_MACRO(a, b) into... SOME_MACRO\(a,\ b\) ... so that it can be passed to a program (not gcc in this case) as a define, eg, some_program -DSOME_MACRO\(a,\ b\)="some expression" I would expect shlex would have this functionality but I didn't find how to do this and checked many similar questions already. I don't mind to write some simple function to do this, but this seems the kind of thing Python would include. Note: the program I'm passing the

MySQL CHAR() Function and UTF8 Output?

时光总嘲笑我的痴心妄想 提交于 2019-11-30 21:45:57
+--------------------------+--------------------------------------------------------+ | Variable_name | Value | +--------------------------+--------------------------------------------------------+ | character_set_client | utf8 | | character_set_connection | utf8 | | character_set_database | utf8 | | character_set_filesystem | binary | | character_set_results | utf8 | | character_set_server | utf8 | | character_set_system | utf8 | | character_sets_dir | /usr/local/mysql-5.1.41-osx10.5-x86_64/share/charsets/ | +--------------------------+--------------------------------------------------------+

How to avoid System.Xml.Linq.XElement escaping HTML content?

蓝咒 提交于 2019-11-30 21:40:09
I'm using the XElement object to build some HTML in the code-behind on an ASP.NET page. I may or may not add some XAttributes to this XElement as I go along, in the following fashion: var elmnt = new XElement("div", new XAttribute("id", "myDiv"), ); Now, if I want to add some content into myDiv which contains HTML, the XElement automatically escapes this which, in my situation, is undesirable. So if I have: var elmnt = new XElement("div", new XAttribute("id", "myDiv"), "<span id='content'>hello world</span>" ); And then I render this into a Placeholder object using the following code:

Uri.EscapeDataString weirdness

半世苍凉 提交于 2019-11-30 21:18:57
Why does EscapeDataString behave differently between .NET 4 and 4.5? The outputs are Uri.EscapeDataString("-_.!~*'()") => "-_.!~*'()" Uri.EscapeDataString("-_.!~*'()") => "-_.%21~%2A%27%28%29" The documentation By default, the EscapeDataString method converts all characters except for RFC 2396 unreserved characters to their hexadecimal representation. If International Resource Identifiers (IRIs) or Internationalized Domain Name (IDN) parsing is enabled, the EscapeDataString method converts all characters, except for RFC 3986 unreserved characters, to their hexadecimal representation. All

json.dumps(): escaping forward slashes

为君一笑 提交于 2019-11-30 20:54:46
Since forward slashes can only occur in strings inside a JSON serialized object and are not escaped (in the default settings), using json.dump(some_dict).replace('/', r'\/') reliably works, but it looks hacky. I know that forward slashes don't have to be escaped, but you may escape them, and for my usecase I'd like to have them escaped. Is there a way to to let the JSONEncoder escape forward slashes without manually escaping them? bozzmob Only escape forward slashes when encode_html_chars=True Check out this- https://github.com/esnme/ultrajson/pull/114 The JSON spec says forward slashes shall

PHP - Getting rid of curly apostrophes

筅森魡賤 提交于 2019-11-30 19:50:12
I'm trying to get rid of curly apostrophes (ones pasted from some sort of rich text doc, I imagine) and I seem to be hitting a road block. The code below isn't working for me. $word = "Today’s"; $search = array('„', '“', '&#146;'); $replace = array('"', '"', "'"); $word = str_replace($search, $replace, htmlentities($word, ENT_QUOTES)); What I end up with is $word containing 'Today&#146;s'. When I remove the ampersands from my $search array, the replace takes place but this, obviously, will not get the job done since the ampersand is left in the string. Why is str_replace failing when it comes