json互转jsonl,log-json

 json转jsonl

import json

def json_to_jsonl(json_file_path, jsonl_file_path):
    with open(json_file_path, 'r', encoding='utf-8') as json_file:
        data = json.load(json_file)

    with open(jsonl_file_path, 'w', encoding='utf-8') as jsonl_file:
        for entry in data:
            jsonl_file.write(json.dumps(entry, ensure_ascii=False) + '\n')

if __name__ == "__main__":
    json_file_path = "json.json" 
    jsonl_file_path = "output_jsonl.jsonl"

    json_to_jsonl(json_file_path, jsonl_file_path)

jsonl转json文件

import json

def convert_jsonl_to_json(jsonl_file_path, json_file_path):
    new_data = []

    with open(jsonl_file_path, 'r', encoding='utf-8') as jsonl_file:
        for line in jsonl_file:
            data = json.loads(line)

            question = data.get("prompt", "")

            new_entry = {
                "id": len(new_data) + 1,
                "part_id": "",
                "dialog": question,
                "target": "其他",
            }

            new_data.append(new_entry)

    with open(json_file_path, 'w', encoding='utf-8') as json_file:
        json.dump(new_data, json_file, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    jsonl_file_path = "train.jsonl"
    json_file_path = "output.json"

    convert_jsonl_to_json(jsonl_file_path, json_file_path)

 json转表格

import pandas as pd
import json
def json_to_excel(json_file_path, excel_file_path):
    with open(json_file_path, 'r', encoding='utf-8') as json_file:
        data = json.load(json_file)

    df = pd.json_normalize(data)
    df.to_excel(excel_file_path, index=False)

if __name__ == "__main__":
    json_file_path = "new.json"
    excel_file_path = "output.xlsx"

    json_to_excel(json_file_path, excel_file_path)

 读取log文件,匹配写入json

import re
import json

def parse_log(log_file_path):
    log_entries = []

    with open(log_file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

        for i in range(len(lines)):
            line = lines[i].strip() # 获取当前行,并去掉首尾的空格和换行符。


            if "INFO - req_data: " in line:
# 如果包含上述字符串,使用正则表达式在当前行中搜索匹配 "INFO - req_data: " 后面的 JSON 数据。`match` 是一个正则匹配对象。
                match = re.search(r"INFO - req_data: ({.*})", line)
                if match:
                    try:
                        req_data_str = match.group(1)#从匹配中提取出 JSON 数据的字符串形式。

                        output_line = lines[i + 1].strip()#获取下一行,即 "INFO - output:" 所在的行。
                        if "INFO - output:" in output_line:
#如果包含上述字符串,提取出 "INFO - output: " 后面的文本内容。

                            output_text = output_line.split("INFO - output: ")[1]

                            entry = {"INFO": req_data_str, "output": output_text}
                            log_entries.append(entry)
                    except json.JSONDecodeError as e:
                        print(line)
                        print(f"Error decoding JSON: {e}")

    return log_entries


def save_to_json(log_entries, output_json_path):
    with open(output_json_path, 'w', encoding='utf-8') as json_file:
        json.dump(log_entries, json_file, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    log_file_path = "run.log"
    output_json_path = "25.json"

    log_entries = parse_log(log_file_path)
    save_to_json(log_entries, output_json_path)





=================================================
import re
import json


def parse_log(log_file_path):
    log_entries = []

    with open(log_file_path, 'r', encoding='utf-8') as file:
        lines = file.readlines()

        for i in range(len(lines)):
            line = lines[i].strip()

            if "INFO - req_data: " in line:
                match = re.search(r"INFO - req_data: ({.*})", line)
                if match:
                    json_str = re.sub(r"(['\"])([^'\"]*?)\1", r'"\2"', match.group(1))

                    try:
                        req_data = json.loads(json_str)

                        output_line = lines[i + 1].strip()
                        if "INFO - output:" in output_line:
                            output_text = output_line.split("INFO - output: ")[1]

                            entry = {"INFO": req_data.get("user_info", ""), "output": output_text}
                            log_entries.append(entry)
                    except json.JSONDecodeError as e:
                        print(line)
                        print(f"Error decoding JSON: {e}")

    return log_entries


def save_to_json(log_entries, output_json_path):
    with open(output_json_path, 'w', encoding='utf-8') as json_file:
        json.dump(log_entries, json_file, ensure_ascii=False, indent=2)


if __name__ == "__main__":
    log_file_path = "a.log"
    output_json_path = "b.json"

    log_entries = parse_log(log_file_path)
    save_to_json(log_entries, output_json_path)



==================================================

import json

def process_json(json_file_path, new_json_file_path):
    new_entries = []

    with open(json_file_path, 'r', encoding='utf-8') as json_file:
        data = json.load(json_file)

    for entry in data:

        info_value = entry.get("INFO", "")
        system_info_position = info_value.find("'system_info'")

        if system_info_position != -1:

            new_info_value = info_value[:system_info_position]

            new_entry = {"INFO": new_info_value, "output": entry.get("output", "")}
            new_entries.append(new_entry)

    with open(new_json_file_path, 'w', encoding='utf-8') as new_json_file:
        json.dump(new_entries, new_json_file, ensure_ascii=False, indent=2)

if __name__ == "__main__":
    json_file_path = "a.json"
    new_json_file_path = "b.json"

    process_json(json_file_path, new_json_file_path)

统计字典分类数量


import json
from collections import Counter

def count_target_categories(json_file_path):
    with open(json_file_path, 'r', encoding='utf-8') as json_file:
        data = json.load(json_file)

    target_counter = Counter()
    print("sum:",len(data))

    for entry in data:
        target_category = entry.get("语言", "")

        target_counter[target_category] += 1

    return target_counter

if __name__ == "__main__":
    json_file_path = "download_25.json" 

    target_count = count_target_categories(json_file_path)

    for category, count in target_count.items():
        print(f"{category}: {count}")

  • 8
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Mybatis-plus支持读取json类型数据,方法如下: 1. 首先,在数据库中创建一个json类型的字段,如下所示: CREATE TABLE `user` ( `id` int(11) NOT NULL AUTO_INCREMENT, `name` varchar(255) DEFAULT NULL, `address` varchar(255) DEFAULT NULL, `info` json DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 2. 在Java实体类中,为info字段添加注解@JSONField,如下所示: public class User { private Long id; private String name; private String address; @JSONField(serializeUsing = JSONObjectSerializer.class, deserializeUsing = JSONObjectDeserializer.class) private JSONObject info; // 省略getter和setter方法 } 3. 在Mybatis-plus的mapper接口中,使用@Results和@Result注解映射json字段,如下所示: @Results(id = "userResultMap", value = { @Result(property = "id", column = "id", id = true), @Result(property = "name", column = "name"), @Result(property = "address", column = "address"), @Result(property = "info", column = "info", javaType = JSONObject.class, typeHandler = JSONTypeHandler.class), }) public interface UserMapper extends BaseMapper<User> {} 4. 在配置文件中,配置typeHandlersPackage为org.apache.ibatis.type,如下所示: mybatis-plus: configuration: map-underscore-to-camel-case: true log-impl: org.apache.ibatis.logging.stdout.StdOutImpl type-handlers-package: org.apache.ibatis.type 5. 最后,就可以在mapper接口中使用Mybatis-plus提供的方法读取json类型数据了,如下所示: User user = userMapper.selectOne(Wrappers.<User>lambdaQuery().eq(User::getId, 1L)); JSONObject info = user.getInfo(); 以上就是Mybatis-plus读取json类型数据的方法实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值