-
Notifications
You must be signed in to change notification settings - Fork 105
Open
Description
其中的mode表示是否换行保存text内容。
import json
json_file = r"G:\Desktop\python小项目\调用微信ocr进行识别文字\2.png.json"
save_file = "save.txt"
def save_text(json_file, save_file, mode=1):
# 打开 JSON 文件
with open(json_file, 'r', encoding='utf-8') as file:
# 从文件中加载 JSON 数据
data = json.load(file)
# 换行保存
if mode == 1:
with open(save_file, 'w', encoding='utf-8') as f:
# 提取每个对象的 text 字段
for item in data['ocrResult']:
print(item['text'])
f.write(item['text'] + '\n')
# 不换行保存
if mode == 2:
with open(save_file, 'w', encoding='utf-8') as f:
# 提取每个对象的 text 字段
for item in data['ocrResult']:
print(item['text'])
f.write(item['text'])
save_text(json_file, save_file, mode=1)Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
No labels