文本数据格式转换备份

概述

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

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)
相关推荐
春风霓裳11 分钟前
sql-窗口函数
大数据·数据库·sql
言之。17 分钟前
【数据库】TiDB 技术选型与架构分析报告
数据库·架构·tidb
人工智能训练44 分钟前
如何在 Ubuntu 22.04 中安装 Docker 引擎和 Linux 版 Docker Desktop 桌面软件
linux·运维·服务器·数据库·ubuntu·docker·ai编程
胖头鱼的鱼缸(尹海文)1 小时前
数据库管理-第386期 使用OCP部署OceanBase 4.4.1社区版集群(20251107)
数据库·oceanbase
Craaaayon1 小时前
如何选择两种缓存更新策略(写缓存+异步写库;写数据库+异步更新缓存)
java·数据库·redis·后端·缓存·mybatis
一 乐1 小时前
点餐|智能点餐系统|基于java+ Springboot的动端的点餐系统小程序(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·小程序·论文
WarriorTan1 小时前
理解PostgreSQL中的数据块
数据库·postgresql
学好statistics和DS2 小时前
三个好思路:SQL并行化处理、混淆矩阵和特征交叉
数据库·sql·矩阵
唐僧洗头爱飘柔95272 小时前
【GORM(3)】Go的跨时代ORM框架!—— 数据库连接、配置参数;本文从0开始教会如何配置GORM的数据库
开发语言·数据库·后端·golang·gorm·orm框架·dsn
谅望者2 小时前
在 macOS 上使用 Homebrew 安装 MySQL 8.0 完整指南
数据库·sql·mysql