提取JSON文档中的html(split)与markdown(正则法)

背景

利用chatlama的方法生成大量的文本数据,以JSON文件的格式存储,需要清洗这些数据,得到html文件和对应的markdown文件。

25w行json数据如何批量处理,得到想要的数据?

方法

读取和解析 JSON 文件:

  • 逐行读取 JSON 文件,并尝试将每一行解析为 JSON 对象。
  • 如果解析成功,将对象添加到 json_objects 列表中,并增加计数器 json_count
json_objects = []
json_count = 0
with open(json_file_path, 'r', encoding='utf-8') as file:
    for line in file:
        line = line.strip()
        if line:
            try:
                json_obj = json.loads(line)
                json_objects.append(json_obj)
                json_count += 1  # 增加计数器
            except json.JSONDecodeError as e:
                print(f"Error decoding JSON on line: {line}")
                print(e)

利用正则法匹配markdown 

Markdown 表格的每一行通常以竖线 | 开头,并以换行符 \n 结尾。利用正则法匹配,

markdown_pattern = re.compile(r'\|.*?\n')

\|:       匹配竖线字符 |

.*?:     匹配任意字符(除了换行符)零次或多次,非贪婪模式。

\n:      匹配换行符。

保存html和markd

将html_path中的内容作为html和markdown文件的文件名,将answer_mode4.split("```html")[1].split("```")[0].strip()作为html的内容。

for obj in json_objects:
    answer_mode4 = obj.get("answer_mode4", "")
    html_path = obj.get("html_path", "")

    if not html_path or not answer_mode4:
        continue

    filename = os.path.splitext(os.path.basename(html_path))[0]

    try:
        html_code = answer_mode4.split("```html")[1].split("```")[0].strip()
    except IndexError:
        html_code = ""

    markdown_code_matches = markdown_pattern.findall(answer_mode4)
    markdown_code = ''.join(markdown_code_matches).strip()

    if html_code:
        with open(os.path.join(html_folder, f"{filename}.html"), 'w', encoding='utf-8') as html_file:
            html_file.write(html_code)

    if markdown_code:
        with open(os.path.join(markdown_folder, f"{filename}.md"), 'w', encoding='utf-8') as markdown_file:
            markdown_file.write(markdown_code)

完整代码

import os
import json
import re

json_file_path = r'g1138_rerender_echart_total.json'
html_folder = r'html_files1'
markdown_folder = r'markdown_files1'

os.makedirs(html_folder, exist_ok=True)
os.makedirs(markdown_folder, exist_ok=True)

json_objects = []
json_count = 0
with open(json_file_path, 'r', encoding='utf-8') as file:
    for line in file:
        line = line.strip()
        if line:
            try:
                json_obj = json.loads(line)
                json_objects.append(json_obj)
                json_count += 1  # 增加计数器
            except json.JSONDecodeError as e:
                print(f"Error decoding JSON on line: {line}")
                print(e)

markdown_pattern = re.compile(r'\|.*?\n')

for obj in json_objects:
    answer_mode4 = obj.get("answer_mode4", "")
    html_path = obj.get("html_path", "")

    if not html_path or not answer_mode4:
        continue

    filename = os.path.splitext(os.path.basename(html_path))[0]

    try:
        html_code = answer_mode4.split("```html")[1].split("```")[0].strip()
    except IndexError:
        html_code = ""

    markdown_code_matches = markdown_pattern.findall(answer_mode4)
    markdown_code = ''.join(markdown_code_matches).strip()

    if html_code:
        with open(os.path.join(html_folder, f"{filename}.html"), 'w', encoding='utf-8') as html_file:
            html_file.write(html_code)

    if markdown_code:
        with open(os.path.join(markdown_folder, f"{filename}.md"), 'w', encoding='utf-8') as markdown_file:
            markdown_file.write(markdown_code)

print("文件已保存到 'html_files' 和 'markdown_files' 文件夹中。")

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gatinaa

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值