许虎虎 开发者工具集

CSV 转多行数据

将 csv 数据转换为多行数据

在这里输入 csv :
结果:
csv转多行数据

CSV 转多行数据
将 CSV(Comma-Separated Values) 文件转换为 多行数据 的形式,通常是指将 CSV 中的每一行转换为某种结构的数据。你可能希望把每一行的数据按一定的格式输出,例如按对象形式、数据库插入语句或其他结构。

1. CSV 示例
假设有如下的 CSV 文件:

c

id,name,email,age
1,John Doe,john.doe@example.com,25
2,Jane Smith,jane.smith@example.com,30
3,Sam Brown,sam.brown@example.com,22
2. 转换为多行数据
将 CSV 转换为多行数据的形式,可能包括不同格式,以下是几种常见的转换方式:

2.1 转换为 JSON 格式
每一行转换为一个 JSON 对象,结果如下:

json

[
{"id": 1, "name": "John Doe", "email": "john.doe@example.com", "age": 25},
{"id": 2, "name": "Jane Smith", "email": "jane.smith@example.com", "age": 30},
{"id": 3, "name": "Sam Brown", "email": "sam.brown@example.com", "age": 22}
]
2.2 转换为 SQL 插入语句
将每一行转换为一条 SQL 插入语句,结果如下:

sql

INSERT INTO users (id, name, email, age) VALUES (1, 'John Doe', 'john.doe@example.com', 25);
INSERT INTO users (id, name, email, age) VALUES (2, 'Jane Smith', 'jane.smith@example.com', 30);
INSERT INTO users (id, name, email, age) VALUES (3, 'Sam Brown', 'sam.brown@example.com', 22);
2.3 转换为列表格式(Python)
每一行转换为一个列表,结果如下:

python

[
[1, 'John Doe', 'john.doe@example.com', 25],
[2, 'Jane Smith', 'jane.smith@example.com', 30],
[3, 'Sam Brown', 'sam.brown@example.com', 22]
]