kanadeblisst00 / wechat_ocr

使用Python调用微信本地ocr服务
190 stars 55 forks source link

作者大大,examples中可以添加一个读取json中的text内容的py文件,是否更加适合新手学习 #10

Open Knighthood2001 opened 2 months ago

Knighthood2001 commented 2 months ago

其中的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)
kanadeblisst00 commented 2 months ago

这项目不是新手教程,新手想学习应该去看Python教程