Python json.loads 的控制字符无效
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22394235/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Invalid control character with Python json.loads
提问by arsenal
Below is my string that is getting printed out with the below code -
下面是我用下面的代码打印出来的字符串 -
jsonString = data.decode("utf-8")
print jsonString
And below is the string that got printed out on the console -
下面是在控制台上打印出来的字符串 -
{"description":"Script to check testtbeat of TEST 1 server.", "script":"#!/bin/bash\nset -e\n\nCOUNT=60 #number of 10 second timeouts in 10 minutes\nSUM_SYNCS=0\nSUM_SYNCS_BEHIND=0\nHOSTNAME=$hostname \n\nwhile [[ $COUNT -ge \"0\" ]]; do\n\necho $HOSTNAME\n\n#send the request, put response in variable\nDATA=$(wget -O - -q -t 1 http://$HOSTNAME:8080/heartbeat)\n\n#grep $DATA for syncs and syncs_behind\nSYNCS=$(echo $DATA | grep -oE 'num_syncs: [0-9]+' | awk '{print }')\nSYNCS_BEHIND=$(echo $DATA | grep -oE 'num_syncs_behind: [0-9]+' | awk '{print }')\n\necho $SYNCS\necho $SYNCS_BEHIND\n\n#verify conditionals\nif [[ $SYNCS -gt \"8\" && $SYNCS_BEHIND -eq \"0\" ]]; then exit 0; fi\n\n#decrement the counter\nlet COUNT-=1\n\n#wait another 10 seconds\nsleep 10\n\ndone\n"}
But when I load this out using python json.loadsas shown below-
但是当我使用 python 加载它时json.loads,如下所示 -
jStr = json.loads(jsonString)
I am getting this error -
我收到此错误 -
ERROR Invalid control character at: line 1 column 202 (char 202)
I looked at char 202 but I have no idea why that is causing an issue? char 202 in my notepad++ is eI guess.. Or may be I am calculating it wrong
我查看了 char 202,但我不知道为什么会导致问题?我的记事本 ++ 中的字符 202 是e我猜.. 或者我计算错了
Any idea what is wrong? How do I find out which one is causing problem.
知道出了什么问题吗?我如何找出哪个导致问题。
UPDATE:-
更新:-
jsonString = {"description":"Script to check testtbeat of TIER 1 server.", "script":"#!/bin/bash\nset -e\n\nCOUNT=60 #number of 10 second timeouts in 10 minutes\nSUM_SYNCS=0\nSUM_SYNCS_BEHIND=0\nHOSTNAME=$hostname \n\nwhile [[ $COUNT -ge \"0\" ]]; do\n\necho $HOSTNAME\n\n#send the request, put response in variable\nDATA=$(wget -O - -q -t 1 http://$HOSTNAME:8080/heartbeat)\n\n#grep $DATA for syncs and syncs_behind\nSYNCS=$(echo $DATA | grep -oE 'num_syncs: [0-9]+' | awk '{print }')\nSYNCS_BEHIND=$(echo $DATA | grep -oE 'num_syncs_behind: [0-9]+' | awk '{print }')\n\necho $SYNCS\necho $SYNCS_BEHIND\n\n#verify conditionals\nif [[ $SYNCS -gt \"8\" && $SYNCS_BEHIND -eq \"0\" ]]; then exit 0; fi\n\n#decrement the counter\nlet COUNT-=1\n\n#wait another 10 seconds\nsleep 10\n\ndone\n"}
print jsonString[202]
Below error I got -
我得到以下错误 -
KeyError: 202
采纳答案by jfs
There is no error in your json text.
You can get the error if you copy-paste the string into your Python source code as a string literal. In that case \nis interpreted as a single character (newline). You can fix it by using raw-string literals instead (r'', Use triple-quotes r'''..'''to avoid escaping "'quotes inside the string literal).
如果将字符串作为字符串文字复制粘贴到 Python 源代码中,则会出现错误。在这种情况下\n被解释为单个字符(换行符)。您可以通过使用原始字符串文字来修复它(r'', 使用三重引号r'''..'''以避免"'在字符串文字中转义引号)。
回答by Pakman
{"description":"Script to check testtbeat of TEST 1 server.", "script":"#!/bin/bash\nset -e\n\nCOUNT=60 #number of 10 second timeouts in 10 minutes\nSUM_SYNCS=0\nSUM_SYNCS_BEHIND=0\nHOSTNAME=$hostname #dc1dbx1145.dc1.host.com\n\nwhile [[ $COUNT -ge \"0\" ]]; do\n\necho $HOSTNAME\n\n#send the request, put response in variable\nDATA=$(wget -O - -q -t 1 http://$HOSTNAME:8080/heartbeat)\n\n#grep $DATA for syncs and syncs_behind\nSYNCS=$(echo $DATA | grep -oE 'num_syncs: [0-9]+' | awk '{print }')\nSYNCS_BEHIND=$(echo $DATA | grep -oE 'num_syncs_behind: [0-9]+' | awk '{print }')\n\necho $SYNCS\necho $SYNCS_BEHIND\n\n#verify conditionals\nif [[ $SYNCS -gt \"8\" && $SYNCS_BEHIND -eq \"0\" ]]; then exit 0; fi\n\n#decrement the counter\nlet COUNT-=1\n\n#wait another 10 seconds\nsleep 10\n\ndone\n"}
Works for me.
为我工作。
Also, if you get an error like this in the future, a debugging technique you can use is to shorten the string to something that works and slowly add data until it doesn't.
此外,如果您将来遇到这样的错误,您可以使用的调试技术是将字符串缩短为有效的字符串,然后慢慢添加数据,直到无效为止。
回答by Joe Cheng
The control character can be allowed inside a string as follows,
控制字符可以允许在字符串中,如下所示,
json_str = json.loads(jsonString, strict=False)
You can find this in the docs for python 2, or the docs for python 3
您可以在python 2的文档或python 3 的文档中找到它
If strict is false (
Trueis the default), then control characters will be allowed inside strings. Control characters in this context are those with character codes in the 0–31 range, including'\t'(tab),'\n','\r'and'\0'.
如果strict 为false(
True默认值),则字符串中将允许使用控制字符。此上下文中的控制字符是那些字符代码在 0-31 范围内的字符,包括'\t'(tab)'\n'、'\r'和'\0'。

