CSV 到 JSON 数组列表
CSV to JSON list of arrays
我的csv是这样的,只有数字。没有 headers。我必须确定 javascprit (node.js) 代码中每一列的名称。
1364.00,0.15,0.36,-0.13,-3.24,-0.42,-0.15,0.90,0.00,-0.01,0.02,0.26,0.01,-0.04
1374.00,0.30,0.76,-0.25,-3.25,-0.41,-0.13,0.91,0.00,-0.00,0.02,0.26,0.01,-0.04
1384.00,0.45,1.08,-0.35,-3.17,-0.41,-0.10,1.00,-0.00,-0.01,0.02,0.26,0.01,-0.07
节点js代码
const csvFilePath = "test - Cópia.csv"
const csvtojsonV2=require('csvtojson')
csvtojsonV2({
noheader:true,
checkType:true,
headers:["Time","Yaw","Pitch","Roll","Heading","Ax", "Ay", "Az","Gx","Gy", "Gz", "Mx", "My", "Mz"],
colParser:{
"column1": Int32Array,
"column2":Int32Array,
"column3":Int32Array,
"column4":Int32Array,
"column5":Int32Array,
"column6":Int32Array,
"column7":Int32Array,
"column8":Int32Array,
"column9":Int32Array,
"column10":Int32Array,
"column11":Int32Array,
"column12":Int32Array,
"column13":Int32Array,
"column14":Int32Array},
})
.fromFile(csvFilePath)
.then((json) => {
console.log(json)
})
我用上面的代码得到的结果是 json:
[
{
Time: 1364,
Yaw: 0.15,
Pitch: 0.36,
Roll: -0.13,
Heading: -3.24,
Ax: -0.42,
Ay: -0.15,
Az: 0.9,
Gx: 0,
Gy: -0.01,
Gz: 0.02,
Mx: 0.26,
My: 0.01,
Mz: -0.04
},
{
Time: 1374,
Yaw: 0.3,
Pitch: 0.76,
Roll: -0.25,
Heading: -3.25,
Ax: -0.41,
Ay: -0.13,
Az: 0.91,
Gx: 0,
Gy: -0,
Gz: 0.02,
Mx: 0.26,
My: 0.01,
Mz: -0.04
},
{
Time: 1384,
Yaw: 0.45,
Pitch: 1.08,
Roll: -0.35,
Heading: -3.17,
Ax: -0.41,
Ay: -0.1,
Az: 1,
Gx: -0,
Gy: -0.01,
Gz: 0.02,
Mx: 0.26,
My: 0.01,
Mz: -0.07
}
]
我想要一些不同的东西,像这样(下面:每列一个数组字典。
{"Time": [1364.0, 1374.0, 1384.0],
"Yaw": [0.15, 0.3, 0.45],
"Pitch": [0.36, 0.76, 1.08],
"Heading": [-0.13, -0.25, -0.35],
"Roll": [-3.24, -3.25, -3.17],
"Ax": [-0.42, -0.41, -0.41],
"Ay": [-0.15, -0.13, -0.1],
"Az": [0.9, 0.91, 1.0],
"Gx": [0.0, 0.0, -0.0],
"Gy": [-0.01, -0.0, -0.01],
"Gz": [0.02, 0.02, 0.02],
"Mx": [0.26, 0.26, 0.26],
"My": [0.01, 0.01, 0.01],
"Mz": [-0.04, -0.04, -0.07]}
第一种方法
使用你的代码,它使用 csvtojson
库,你可以添加这个:
const csvFilePath = "test - Cópia.csv"
const csvtojsonV2=require('csvtojson')
csvtojsonV2({
noheader:true,
checkType:true,
headers:["Time","Yaw","Pitch","Roll","Heading","Ax", "Ay", "Az","Gx","Gy", "Gz", "Mx", "My", "Mz"],
colParser:{
"column1": Int32Array,
"column2":Int32Array,
"column3":Int32Array,
"column4":Int32Array,
"column5":Int32Array,
"column6":Int32Array,
"column7":Int32Array,
"column8":Int32Array,
"column9":Int32Array,
"column10":Int32Array,
"column11":Int32Array,
"column12":Int32Array,
"column13":Int32Array,
"column14":Int32Array},
})
.fromFile(csvFilePath)
.then((json) => {
const RESULT = json.reduce((acc, v, i) => {
for (const [key, value] of Object.entries(v)) {
if (!acc[key]) acc[key] = [];
acc[key].push(value);
}
return acc;
}, {});
console.log(RESULT)
})
它所做的是在 acc
变量中累积数据,使用每一行的键,这些键是 header,正如您在 acc[key].push(value)
中看到的那样,if (!acc[key]) acc[key] = []
是为了确保在 acc
变量(累加器)中使用空数组初始化标题;
第二种方法
这不使用任何库:
// Arbitrary data (from you example).
const DATA = `1364.00,0.15,0.36,-0.13,-3.24,-0.42,-0.15,0.90,0.00,-0.01,0.02,0.26,0.01,-0.04
1374.00,0.30,0.76,-0.25,-3.25,-0.41,-0.13,0.91,0.00,-0.00,0.02,0.26,0.01,-0.04
1384.00,0.45,1.08,-0.35,-3.17,-0.41,-0.10,1.00,-0.00,-0.01,0.02,0.26,0.01,-0.07
`.trim().split('\n').map(row => row.split(','));
const HEADERS = ["Time", "Yaw", "Pitch", "Roll", "Heading", "Ax", "Ay", "Az", "Gx", "Gy", "Gz", "Mx", "My", "Mz"];
const RESULT = {};
// Assign every heading to the result.
for (const HEADING of HEADERS) RESULT[HEADING] = [];
DATA.map(row =>
// Each row → each column, number of columns === number of headers
row.map((column, i) =>
RESULT[HEADERS[i]]
.push(Number(column))
)
);
console.log(RESULT);
每一步的解释:
- 假设CSV的内容用“\n”分隔行,首选的列分隔符是“,”,我设置了一个任意的数据内容;
- 创建了一个常量
HEADERS
,headers必须与CSV数据列的长度相同;
- 创建常量
RESULT
,这会将解析数据的结果存储到JSON。
- 我们用定义的
HEADERS
和一个空数组初始化RESULT
的键;
- 我们映射每个行然后每个列的数据,使用列的索引来引用我们[=中的当前标题17=] 数组,因为列的长度应等于
HEADERS
常量的长度。
如果您必须保留数字的尾随零(例如:15.00),那么您可以在插入到结果数组。
我的csv是这样的,只有数字。没有 headers。我必须确定 javascprit (node.js) 代码中每一列的名称。
1364.00,0.15,0.36,-0.13,-3.24,-0.42,-0.15,0.90,0.00,-0.01,0.02,0.26,0.01,-0.04
1374.00,0.30,0.76,-0.25,-3.25,-0.41,-0.13,0.91,0.00,-0.00,0.02,0.26,0.01,-0.04
1384.00,0.45,1.08,-0.35,-3.17,-0.41,-0.10,1.00,-0.00,-0.01,0.02,0.26,0.01,-0.07
节点js代码
const csvFilePath = "test - Cópia.csv"
const csvtojsonV2=require('csvtojson')
csvtojsonV2({
noheader:true,
checkType:true,
headers:["Time","Yaw","Pitch","Roll","Heading","Ax", "Ay", "Az","Gx","Gy", "Gz", "Mx", "My", "Mz"],
colParser:{
"column1": Int32Array,
"column2":Int32Array,
"column3":Int32Array,
"column4":Int32Array,
"column5":Int32Array,
"column6":Int32Array,
"column7":Int32Array,
"column8":Int32Array,
"column9":Int32Array,
"column10":Int32Array,
"column11":Int32Array,
"column12":Int32Array,
"column13":Int32Array,
"column14":Int32Array},
})
.fromFile(csvFilePath)
.then((json) => {
console.log(json)
})
我用上面的代码得到的结果是 json:
[
{
Time: 1364,
Yaw: 0.15,
Pitch: 0.36,
Roll: -0.13,
Heading: -3.24,
Ax: -0.42,
Ay: -0.15,
Az: 0.9,
Gx: 0,
Gy: -0.01,
Gz: 0.02,
Mx: 0.26,
My: 0.01,
Mz: -0.04
},
{
Time: 1374,
Yaw: 0.3,
Pitch: 0.76,
Roll: -0.25,
Heading: -3.25,
Ax: -0.41,
Ay: -0.13,
Az: 0.91,
Gx: 0,
Gy: -0,
Gz: 0.02,
Mx: 0.26,
My: 0.01,
Mz: -0.04
},
{
Time: 1384,
Yaw: 0.45,
Pitch: 1.08,
Roll: -0.35,
Heading: -3.17,
Ax: -0.41,
Ay: -0.1,
Az: 1,
Gx: -0,
Gy: -0.01,
Gz: 0.02,
Mx: 0.26,
My: 0.01,
Mz: -0.07
}
]
我想要一些不同的东西,像这样(下面:每列一个数组字典。
{"Time": [1364.0, 1374.0, 1384.0],
"Yaw": [0.15, 0.3, 0.45],
"Pitch": [0.36, 0.76, 1.08],
"Heading": [-0.13, -0.25, -0.35],
"Roll": [-3.24, -3.25, -3.17],
"Ax": [-0.42, -0.41, -0.41],
"Ay": [-0.15, -0.13, -0.1],
"Az": [0.9, 0.91, 1.0],
"Gx": [0.0, 0.0, -0.0],
"Gy": [-0.01, -0.0, -0.01],
"Gz": [0.02, 0.02, 0.02],
"Mx": [0.26, 0.26, 0.26],
"My": [0.01, 0.01, 0.01],
"Mz": [-0.04, -0.04, -0.07]}
第一种方法
使用你的代码,它使用 csvtojson
库,你可以添加这个:
const csvFilePath = "test - Cópia.csv"
const csvtojsonV2=require('csvtojson')
csvtojsonV2({
noheader:true,
checkType:true,
headers:["Time","Yaw","Pitch","Roll","Heading","Ax", "Ay", "Az","Gx","Gy", "Gz", "Mx", "My", "Mz"],
colParser:{
"column1": Int32Array,
"column2":Int32Array,
"column3":Int32Array,
"column4":Int32Array,
"column5":Int32Array,
"column6":Int32Array,
"column7":Int32Array,
"column8":Int32Array,
"column9":Int32Array,
"column10":Int32Array,
"column11":Int32Array,
"column12":Int32Array,
"column13":Int32Array,
"column14":Int32Array},
})
.fromFile(csvFilePath)
.then((json) => {
const RESULT = json.reduce((acc, v, i) => {
for (const [key, value] of Object.entries(v)) {
if (!acc[key]) acc[key] = [];
acc[key].push(value);
}
return acc;
}, {});
console.log(RESULT)
})
它所做的是在 acc
变量中累积数据,使用每一行的键,这些键是 header,正如您在 acc[key].push(value)
中看到的那样,if (!acc[key]) acc[key] = []
是为了确保在 acc
变量(累加器)中使用空数组初始化标题;
第二种方法
这不使用任何库:
// Arbitrary data (from you example).
const DATA = `1364.00,0.15,0.36,-0.13,-3.24,-0.42,-0.15,0.90,0.00,-0.01,0.02,0.26,0.01,-0.04
1374.00,0.30,0.76,-0.25,-3.25,-0.41,-0.13,0.91,0.00,-0.00,0.02,0.26,0.01,-0.04
1384.00,0.45,1.08,-0.35,-3.17,-0.41,-0.10,1.00,-0.00,-0.01,0.02,0.26,0.01,-0.07
`.trim().split('\n').map(row => row.split(','));
const HEADERS = ["Time", "Yaw", "Pitch", "Roll", "Heading", "Ax", "Ay", "Az", "Gx", "Gy", "Gz", "Mx", "My", "Mz"];
const RESULT = {};
// Assign every heading to the result.
for (const HEADING of HEADERS) RESULT[HEADING] = [];
DATA.map(row =>
// Each row → each column, number of columns === number of headers
row.map((column, i) =>
RESULT[HEADERS[i]]
.push(Number(column))
)
);
console.log(RESULT);
每一步的解释:
- 假设CSV的内容用“\n”分隔行,首选的列分隔符是“,”,我设置了一个任意的数据内容;
- 创建了一个常量
HEADERS
,headers必须与CSV数据列的长度相同; - 创建常量
RESULT
,这会将解析数据的结果存储到JSON。 - 我们用定义的
HEADERS
和一个空数组初始化RESULT
的键; - 我们映射每个行然后每个列的数据,使用列的索引来引用我们[=中的当前标题17=] 数组,因为列的长度应等于
HEADERS
常量的长度。
如果您必须保留数字的尾随零(例如:15.00),那么您可以在插入到结果数组。