This repository was archived by the owner on Aug 6, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathcsv-example.ts
More file actions
98 lines (86 loc) · 2.3 KB
/
Copy pathcsv-example.ts
File metadata and controls
98 lines (86 loc) · 2.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
import { readCSV, writeCSV } from '../../src/csv.ts' // replace with latest library https://deno.land/x/flat@0.0.x/mod.ts
// Path to a csv file
const csvPath = './examples/csv/prices.csv';
/*
Parse a csv file and return an object[]
[
{ Name: "One", Amount: "500", Price: "$0.5" },
{ Name: "Two", Amount: "13", Price: "$10" },
{ Name: "Three", Amount: "-3", Price: "$3000" }
]
*/
const originalCSV = await readCSV(csvPath)
console.log(originalCSV)
/*
Can use other options for reading CSV
More detail on options can be found here: https://deno.land/std@0.92.0/encoding#csv
*/
const csvOptions = await readCSV(csvPath, {
separator: ',', // can use an optional separator. default is comma
trimLeadingSpace: false, // whether to trim the leading space. default is false
lazyQuotes: false // Allow unquoted quote in a quoted field or non double quoted quotes in quoted field. default is false
})
console.log(csvOptions);
/*
Parse a CSV file, skip the first row, and rename the column headers. Return an object[]
[
{ id: "One", quantity: "500", cost: "$0.5" },
{ id: "Two", quantity: "13", cost: "$10" },
{ id: "Three", quantity: "-3", cost: "$3000" }
]
*/
const renameColumnsCSV = await readCSV(csvPath, {
columns: ['id', 'quantity', 'cost'],
});
console.log(renameColumnsCSV);
/*
Parse a CSV file, skip the first row, and apply a custom function to the second 'quantity' row. Returns an object[]
[
{ id: "One", quantity: "50", cost: "$0.5" },
{ id: "Two", quantity: "1.3", cost: "$10" },
{ id: "Three", quantity: "-0.3", cost: "$3000" }
]
*/
const parseColumnCSV = await readCSV(csvPath, {
columns: [
{
name: 'id'
},
{
name: 'quantity',
parse: (e: string): string => {
return `${parseFloat(e) / 10}`
}
},
{
name: 'cost'
}
],
});
console.log(parseColumnCSV)
/*
Write data to a CSV file
age,name
70,Rick
14,Smith
*/
const data = [
{
age: 70,
name: 'Rick'
},
{
age: 14,
name: 'Smith'
}
]
writeCSV('./examples/csv/names.csv', data)
/*
Write one of the previously parsed csv examples
id,quantity,cost
One,50,$0.5
Two,1.3,$10
Three,-0.3,$3000
*/
console.log(parseColumnCSV)
writeCSV('./examples/csv/prices-write.csv', parseColumnCSV)