使用Python json.load无效的控制字符

时间:2022-05-18 06:28:36

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 $2}')\nSYNCS_BEHIND=$(echo $DATA | grep -oE 'num_syncs_behind: [0-9]+' | awk '{print $2}')\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.loads as shown below-

但是当我使用python json加载时。加载如下所示

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 e I guess.. Or may be I am calculating it wrong

我查看了char 202但是我不知道为什么会引起问题?在我的记事本++ +中char 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 $2}')\nSYNCS_BEHIND=$(echo $DATA | grep -oE 'num_syncs_behind: [0-9]+' | awk '{print $2}')\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

3 个解决方案

#1


16  

There is no error in your json text.

json文本中没有错误。

You can get the error if you copy-paste the string into your Python source code as a string literal. In that case \n is 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'。“'以避免转义''在字符串文字内的引号)。

#2


44  

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, or the docs for python 3

您可以在docs或python 3的docs中找到

If strict is false (True is 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'.

如果严格为假(默认为真),那么控制字符将被允许在字符串中。在此上下文中,控制字符是那些具有0 - 31范围内的字符代码的字符,包括'\t' (tab)、'\n'、'\r'和'\0'。

#3


-1  

Escape your newlines.

逃避你的换行。

{"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 $2}')\\nSYNCS_BEHIND=$(echo $DATA | grep -oE 'num_syncs_behind: [0-9]+' | awk '{print $2}')\\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.

另外,如果将来出现这样的错误,可以使用的调试技术是将字符串缩短为可以工作的对象,并缓慢地添加数据,直到它不工作为止。

#1


16  

There is no error in your json text.

json文本中没有错误。

You can get the error if you copy-paste the string into your Python source code as a string literal. In that case \n is 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'。“'以避免转义''在字符串文字内的引号)。

#2


44  

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, or the docs for python 3

您可以在docs或python 3的docs中找到

If strict is false (True is 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'.

如果严格为假(默认为真),那么控制字符将被允许在字符串中。在此上下文中,控制字符是那些具有0 - 31范围内的字符代码的字符,包括'\t' (tab)、'\n'、'\r'和'\0'。

#3


-1  

Escape your newlines.

逃避你的换行。

{"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 $2}')\\nSYNCS_BEHIND=$(echo $DATA | grep -oE 'num_syncs_behind: [0-9]+' | awk '{print $2}')\\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.

另外,如果将来出现这样的错误,可以使用的调试技术是将字符串缩短为可以工作的对象,并缓慢地添加数据,直到它不工作为止。