• Python, Websockets, and commands with quotes
    4 replies, posted
I'm trying to do something that involves sending commands to a server, however, these commands have quotes in them. What I fear is the fact if I want to let it process a command such as " JCH {"channel": "Frontpage"} " and put it in some form of code like so. [code]ws.send("JCH {"channel": "Frontpage"}")[/code] It wouldn't process it correctly. Added +: Oh I'm a fucknut. [CODE]ws.send("""JCH {"channel": "Frontpage"}""")[/CODE] That is what I needed to do.
Alternatively, [url=https://en.wikipedia.org/wiki/Escape_character#Programming_and_data_formats]escape[/url] those quotes.
Alternatively, you can just enclose a string in single quotes if it contains double quotes, or the other way around, enclose it in double quotes if it contains single quotes.
JS [CODE]ws.send(JSON.stringify(someobject))[/CODE] Python [CODE]sock.send(json.dumps(response))[/CODE]
Use single quotes to enclose the string, and use double quotes inside the string.. [code] print 'here is a quotation > ", pretty cool eh?' [/code] Or vice versa.
Sorry, you need to Log In to post a reply to this thread.