python-3.x - 使用 Python 编辑 JSON 响应
问题描述
我正在使用 Cognitive Read API 从图像中提取文本。我收到如下回复:
{
"status": "Succeeded",
"recognitionResult": {
"lines": [
{
"boundingBox": [
2,
52,
65,
46,
69,
89,
7,
95
],
"text": "$230",
"words": [
{
"boundingBox": [
0,
59,
63,
43,
77,
86,
3,
102
],
"text": "$230"
}
]
},
{
"boundingBox": [
6,
2,
771,
13,
770,
75,
5,
64
],
"text": "The quick brown fox jumps over the lazy",
"words": [
{
"boundingBox": [
0,
4,
92,
5,
77,
71,
0,
71
],
"text": "The"
},
{
"boundingBox": [
74,
4,
189,
5,
174,
72,
60,
71
],
"text": "quick"
},
{
"boundingBox": [
176,
5,
321,
6,
306,
73,
161,
72
],
"text": "brown"
},
{
"boundingBox": [
308,
6,
387,
6,
372,
73,
293,
73
],
"text": "fox"
},
{
"boundingBox": [
382,
6,
506,
7,
491,
74,
368,
73
],
"text": "jumps"
},
{
"boundingBox": [
492,
7,
607,
8,
592,
75,
478,
74
],
"text": "over"
},
{
"boundingBox": [
589,
8,
673,
8,
658,
75,
575,
75
],
"text": "the"
},
{
"boundingBox": [
660,
8,
783,
9,
768,
76,
645,
75
],
"text": "lazy"
}
]
},
{
"boundingBox": [
2,
84,
783,
96,
782,
154,
1,
148
],
"text": "$78,000.00",
"words": [
{
"boundingBox": [
0,
86,
94,
87,
72,
151,
0,
149
],
"text": "$78,000.00"
},
{
"boundingBox": [
76,
87,
164,
88,
142,
152,
54,
150
],
"text": "my"
},
{
"boundingBox": [
155,
88,
243,
89,
222,
152,
134,
151
],
"text": "box"
},
{
"boundingBox": [
226,
89,
344,
90,
323,
154,
204,
152
],
"text": "with"
},
{
"boundingBox": [
336,
90,
432,
91,
411,
154,
314,
154
],
"text": "five"
},
{
"boundingBox": [
419,
91,
538,
92,
516,
154,
398,
154
],
"text": "dozen"
},
{
"boundingBox": [
547,
92,
701,
94,
679,
154,
525,
154
],
"text": "liquor"
},
{
"boundingBox": [
696,
94,
800,
95,
780,
154,
675,
154
],
"text": "jugs"
}
]
}
]
}
}
现在我的要求是用 XXXXX 掩盖 Json 响应中的所有金额($230,$78,000.00)或涂黑这些数字。我正在使用 azure notebooks 在 python 3.6 中构建代码。如果有人在此之前工作过,请提供帮助。
谢谢你!
解决方案
您无法控制 JSON 响应,但您可以控制从响应中向用户显示的内容!
您可以检查返回的每个文本是否包含该$
字符并删除它之后的所有内容!
这是一个例子:
text = 'some text$ this part will be removed.'
head, sep, tail = text.partition('$')
print(head)
将显示“一些文字”
print(sep)
将显示“$”
print(tail)
将显示“这部分将被删除。”
推荐阅读
- javascript - 如何使用 gtag.js 禁用新 GA4 属性的协议检查?
- c - 导致分段错误的英特尔 SIMD 指令
- google-apps-script - 我们可以从 google appscript 打开命令提示符吗
- botframework - 新闻样本技能返回未经授权的错误
- android - 如何在 Android 上删除标题栏
- r - 如何根据字符串模式将数据框的所有列拆分为不同的组并将函数应用于每组列?
- amazon-web-services - 如何配置通过 VPC 从 aws ec2 实例访问我的 aws s3 存储桶
- elasticsearch - 一些 KV 过滤器值具有在 Kibana 中标识为字符串的自定义日期
- json - 将规则添加到 JSON 字符串化
- maven - Maven 错误“maven-clean-plugin:2.5 或其依赖项之一无法解析:无法读取工件描述符”