简单 python 正则表达式
Simple python regex
我有一个文本文件,我想替换以下模式:
\"
与:
"
我正在查看的初始版本如下所示:
{"latestProfileVersion":51,
"scannerAccess":true,
"productRatings":"[{\"7H65018000\":{\"reviewCount\":0,\"avgRating\":0}}
所以有人在 JSON 响应中嵌入了 JSON 字符串。
这是我目前拥有的:
rawAuthResponseTextFile = open(rawAuthResponseFilename,'r')
formattedAuthResponse = open('formattedAuthResponse.txt', 'w')
try:
stringVersionOfAuthResponse = rawAuthResponseTextFile.read().replace('\n','')
cleanedStringVersionOfAuthResponse = re.sub(r'\"', '"', stringVersionOfAuthResponse)
jsonVersionOfAuthResponse = json.dumps(cleanedStringVersionOfAuthResponse)
formattedAuthResponse.write(jsonVersionOfAuthResponse)
finally:
rawAuthResponseTextFile.close()
formattedAuthResponse.close
使用 http://pythex.org/ 我发现 r'\"'
应该只匹配 \"
,但是当我查看输出似乎正在添加额外的转义字符。
我知道我做错了什么,因为我无法让嵌入字符串周围的引号看起来像常规 JSON 中的引号,无论我如何调整它,是否转义字符。
您需要使用这个正则表达式
\"
您需要使用 \
转义 \
我有一个文本文件,我想替换以下模式:
\"
与:
"
我正在查看的初始版本如下所示:
{"latestProfileVersion":51,
"scannerAccess":true,
"productRatings":"[{\"7H65018000\":{\"reviewCount\":0,\"avgRating\":0}}
所以有人在 JSON 响应中嵌入了 JSON 字符串。
这是我目前拥有的:
rawAuthResponseTextFile = open(rawAuthResponseFilename,'r')
formattedAuthResponse = open('formattedAuthResponse.txt', 'w')
try:
stringVersionOfAuthResponse = rawAuthResponseTextFile.read().replace('\n','')
cleanedStringVersionOfAuthResponse = re.sub(r'\"', '"', stringVersionOfAuthResponse)
jsonVersionOfAuthResponse = json.dumps(cleanedStringVersionOfAuthResponse)
formattedAuthResponse.write(jsonVersionOfAuthResponse)
finally:
rawAuthResponseTextFile.close()
formattedAuthResponse.close
使用 http://pythex.org/ 我发现 r'\"'
应该只匹配 \"
,但是当我查看输出似乎正在添加额外的转义字符。
我知道我做错了什么,因为我无法让嵌入字符串周围的引号看起来像常规 JSON 中的引号,无论我如何调整它,是否转义字符。
您需要使用这个正则表达式
\"
您需要使用 \
\