文本数据格式转换备份

概述

一些文本格式转换代码,个人做一下备份。

ipynb转py格式

python 复制代码
import nbformat
from nbconvert import PythonExporter

# 读取 Jupyter notebook 文件
notebook_filename = 'Builddata.ipynb'  # 替换为你的 .ipynb 文件路径
with open(notebook_filename, 'r', encoding='utf-8') as notebook_file:
    notebook_content = nbformat.read(notebook_file, as_version=4)

# 使用 PythonExporter 将 notebook 转换为 Python 脚本
python_exporter = PythonExporter()
python_code, _ = python_exporter.from_notebook_node(notebook_content)

# 将 Python 代码写入 .py 文件
python_filename = notebook_filename.replace('.ipynb', '.py')
with open(python_filename, 'w', encoding='utf-8') as python_file:
    python_file.write(python_code)

print(f'Notebook converted to Python script: {python_filename}')

CSV格式转数据库db格式

python 复制代码
import sqlite3
import csv

# 你的CSV文件路径
csv_file = '202103_LD.csv'

# 转成SQLite数据库文件路径
db_file = '202103_LD.db'

# 连接SQLite数据库(如果数据库文件不存在,它会被自动创建)
conn = sqlite3.connect(db_file)
cursor = conn.cursor()

# 读取CSV文件并获取列名
with open(csv_file, mode='r', newline='', encoding='utf-8') as file:
    reader = csv.reader(file)
    headers = next(reader)  # 获取第一行(标签行)

    # 动态创建SQL表语句 data为表名
    columns = ', '.join([f"{header} TEXT" for header in headers])
    create_table_sql = f"CREATE TABLE IF NOT EXISTS data ({columns})"
    cursor.execute(create_table_sql)

    # 插入数据
    for row in reader:
        placeholders = ', '.join(['?' for _ in headers])
        insert_sql = f"INSERT INTO data ({', '.join(headers)}) VALUES ({placeholders})"
        cursor.execute(insert_sql, row)

# 提交事务并关闭连接
conn.commit()
conn.close()

print(f"数据成功从CSV文件导入到数据库 {db_file} 中!")

CSV格式转json格式

python 复制代码
import csv
import json

csv_file = 'data.csv'
json_file = 'data.json'

# 读取CSV文件
with open(csv_file, mode='r', newline='', encoding='utf-8') as csvf:
    reader = csv.DictReader(csvf)
    data = [row for row in reader]

# 写入JSON文件
with open(json_file, 'w', encoding='utf-8') as jsonf:
    json.dump(data, jsonf, ensure_ascii=False, indent=4)

json格式转CSV格式

python 复制代码
import json
import csv

json_file = 'data.json'
csv_file = 'data.csv'

# 读取JSON文件
with open(json_file, 'r', encoding='utf-8') as jf:
    data = json.load(jf)

# 写入CSV文件
with open(csv_file, mode='w', newline='', encoding='utf-8') as cf:
    writer = csv.DictWriter(cf, fieldnames=data[0].keys())
    writer.writeheader()
    writer.writerows(data)
相关推荐
RestCloud9 小时前
SQL Server到Hive:批处理ETL性能提升30%的实战经验
数据库·api
RestCloud9 小时前
为什么说零代码 ETL 是未来趋势?
数据库·api
ClouGence12 小时前
CloudCanal + Paimon + SelectDB 从 0 到 1 构建实时湖仓
数据库
DemonAvenger18 小时前
NoSQL与MySQL混合架构设计:从入门到实战的最佳实践
数据库·mysql·性能优化
AAA修煤气灶刘哥1 天前
后端人速藏!数据库PD建模避坑指南
数据库·后端·mysql
RestCloud1 天前
揭秘 CDC 技术:让数据库同步快人一步
数据库·api
得物技术2 天前
MySQL单表为何别超2000万行?揭秘B+树与16KB页的生死博弈|得物技术
数据库·后端·mysql
可涵不会debug2 天前
【IoTDB】时序数据库选型指南:工业大数据场景下的技术突围
数据库·时序数据库
ByteBlossom2 天前
MySQL 面试场景题之如何处理 BLOB 和CLOB 数据类型?
数据库·mysql·面试
麦兜*2 天前
MongoDB Atlas 云数据库实战:从零搭建全球多节点集群
java·数据库·spring boot·mongodb·spring·spring cloud