Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Python中将Json字符串拆分为单个字符串_Python_Json_Regex - Fatal编程技术网

在Python中将Json字符串拆分为单个字符串

在Python中将Json字符串拆分为单个字符串,python,json,regex,Python,Json,Regex,我正在学习用python编写带有json和re的日志解析脚本。 需要读取日志文件,找到有Json响应的行,解析并以表格形式打印。 我想使用finditer获取每个子字符串,但我的代码匹配的是整个连接的字符串 Code: for m in re.finditer(r'{"APIResponse".*"Type":"\w+"}}',line,re.I): print (m.group(0)) 但包含Json字符串的行有时会以以下格式连接: {"APIResponse":{"ResponseC

我正在学习用python编写带有json和re的日志解析脚本。 需要读取日志文件,找到有Json响应的行,解析并以表格形式打印。 我想使用finditer获取每个子字符串,但我的代码匹配的是整个连接的字符串

Code:
for m in re.finditer(r'{"APIResponse".*"Type":"\w+"}}',line,re.I):
   print (m.group(0))
但包含Json字符串的行有时会以以下格式连接:

{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"internet"}}{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"directdebit"}}Pament Request Output from Server ....
{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"directdebit"}}{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"directdebit"}}2018-07-09 10:01:18 DEBUG PaymentGatewayICSClient:981 - ClientRef         = 1587604390003
{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"internet"}}{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"internet"}}2018-07-09 10:01:18 DEBUG PaymentGatewayICSClient:981 - ClientRef         = 158760439AX00
{"APIResponse":{"ResponseCode"-1,"ResponseText":"Fail9"},"TxnResp":{"Type":"directdebit"}}{"APIResponse":{"ResponseCode":101,"ResponseText":"Success"},"TxnResp":{"Type":"directdebit"}}Transaction Denied

您当前的正则表达式是一个贪婪的正则表达式,这意味着它试图匹配尽可能长的字符串;如果线路是

{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"internet"}}{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"directdebit"}}Pament Request Output from Server ....
运行

for m in re.finditer(r'{"APIResponse".*"Type":"\w+"}}',line,re.I):
   print (m.group(0))
将给出结果

{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"internet"}}{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"directdebit"}}
要使正则表达式以惰性方式运行,以便返回尽可能短的字符串,请在
*
字符后添加

for m in re.finditer(r'{"APIResponse".*?"Type":"\w+"}}',line,re.I):
   print (m.group(0))
输出:

{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"internet"}}
{"APIResponse":{"ResponseCode":0,"ResponseText":"Success"},"TxnResp":{"Type":"directdebit"}}

是了解正则表达式如何工作的极好资源。

您如何生成
,您想得到什么结果?您的正则表达式应该用于提取行的JSON部分。我认为您只需要添加。所以您的正则表达式看起来像{“APIResponse”*?“Type”:“\w+”}}我以前尝试过这个,但忽略了输出。现在工作很好。并且一定会通过你共享的正则表达式资源。