pandas的文本与序列化

文章目录

1.pandas的文本与序列化

python 复制代码
result_data = pd.DataFrame(json_data_list)

with open(jsonl_file_path, 'w', encoding='utf-8') as jsonl_file:
    result_data.to_json(orient='records', lines=True, force_ascii=False, path_or_buf=jsonl_file)
python 复制代码
数据不换行
df.at[i, column_name_transcript] = df.at[i, column_name_transcript].split('\n')

pandas转序列化数据

python 复制代码
data_dicts = df.to_dict(orient='records')

with open(jsonl_file_path, 'w', encoding='utf-8') as jsonl_file:
    for data in data_dicts:
        # 将字典转换为JSON字符串,ensure_ascii=False参数确保中文字符不会被转义
        # 写入文件时,每个JSON对象后面跟着一个换行符
        jsonl_file.write(json.dumps(data, ensure_ascii=False) + '\n')

pandas元素序列化

python 复制代码
    df['column_01'] = df['column_01'].apply(
        lambda x: json.dumps(x, ensure_ascii=False) if isinstance(x, str) else ''
    )
python 复制代码
 # 对"answer"列中的每个字符串元素去除空白并分割成单词列表
    df['question'] = df['question'].apply(lambda x: x.strip().split())
    df['answer'] = df['answer'].apply(lambda x: x.strip().split())

    # 序列化"answer"列中的每个元素为JSON格式的字符串
    df['question'] = df['question'].apply(lambda x: json.dumps(x, ensure_ascii=False) if isinstance(x, list) else x)
    df['answer'] = df['answer'].apply(lambda x: json.dumps(x, ensure_ascii=False) if isinstance(x, list) else x)
相关推荐
ValhallaCoder2 小时前
hot100-二叉树I
数据结构·python·算法·二叉树
猫头虎3 小时前
如何排查并解决项目启动时报错Error encountered while processing: java.io.IOException: closed 的问题
java·开发语言·jvm·spring boot·python·开源·maven
八零后琐话3 小时前
干货:程序员必备性能分析工具——Arthas火焰图
开发语言·python
青春不朽5124 小时前
Scrapy框架入门指南
python·scrapy
MZ_ZXD0015 小时前
springboot旅游信息管理系统-计算机毕业设计源码21675
java·c++·vue.js·spring boot·python·django·php
全栈老石6 小时前
Python 异步生存手册:给被 JS async/await 宠坏的全栈工程师
后端·python
梨落秋霜6 小时前
Python入门篇【模块/包】
python
阔皮大师7 小时前
INote轻量文本编辑器
java·javascript·python·c#
小法师爱分享7 小时前
StickyNotes,简单便签超实用
java·python