1- import { Column , DataItem , stringify } from 'https://deno.land/[email protected] /encoding/csv.ts' ; 2- import { readCSV , writeCSV } from 'https://deno.land/x/[email protected] /mod.ts' 1+ import { readCSV , writeCSV } from '../../csv.ts'
32
43// Path to a csv file
54const csvPath = './examples/csv/prices.csv' ;
65
76/*
8- Parse a csv file and return a string[] []
7+ Parse a csv file and return an object []
98
109[
11- [ "Name", "Amount", "Price" ],
12- [ "One", "500", "$0.5" ],
13- [ "Two", "13", "$10" ],
14- [ "Three", "-3", "$3000" ]
10+ { Name: "One", Amount: "500", Price: "$0.5" },
11+ { Name: "Two", Amount: "13", Price: "$10" },
12+ { Name: "Three", Amount: "-3", Price: "$3000" }
1513]
1614*/
1715const originalCSV = await readCSV ( csvPath )
1816console . log ( originalCSV )
1917
2018/*
21- Parse a CSV file and skip the first row. Return an object[]
22-
23- [
24- { Name: "One", Amount: "500", Price: "$0.5" },
25- { Name: "Two", Amount: "13", Price: "$10" },
26- { Name: "Three", Amount: "-3", Price: "$3000" }
27- ]
19+ Can use other options for reading CSV
20+ More detail on options can be found here: https://deno.land/[email protected] /encoding#csv 2821*/
29- const skipFirstRowCSV = await readCSV ( csvPath , {
30- skipFirstRow : true
31- // separator: ',' // can use an optional separator. default is comma
32- // trimLeadingSpace: false, // whether to trim the leading space. default is false
33- // lazyQuotes: false // Allow unquoted quote in a quoted field or non double quoted quotes in quoted field. default is false
22+ const csvOptions = await readCSV ( csvPath , {
23+ separator : ',' , // can use an optional separator. default is comma
24+ trimLeadingSpace : false , // whether to trim the leading space. default is false
25+ lazyQuotes : false // Allow unquoted quote in a quoted field or non double quoted quotes in quoted field. default is false
3426} )
35- console . log ( skipFirstRowCSV ) ;
27+ console . log ( csvOptions ) ;
3628
3729/*
3830Parse a CSV file, skip the first row, and rename the column headers. Return an object[]
@@ -44,7 +36,6 @@ Parse a CSV file, skip the first row, and rename the column headers. Return an o
4436]
4537*/
4638const renameColumnsCSV = await readCSV ( csvPath , {
47- skipFirstRow : true ,
4839 columns : [ 'id' , 'quantity' , 'cost' ] ,
4940} ) ;
5041console . log ( renameColumnsCSV ) ;
@@ -59,7 +50,6 @@ Parse a CSV file, skip the first row, and apply a custom function to the second
5950]
6051*/
6152const parseColumnCSV = await readCSV ( csvPath , {
62- skipFirstRow : true ,
6353 columns : [
6454 {
6555 name : 'id'
@@ -78,27 +68,23 @@ const parseColumnCSV = await readCSV(csvPath, {
7868console . log ( parseColumnCSV )
7969
8070/*
81- Write data to a file with a header row
71+ Write data to a CSV file
8272
83- name, age
84- Rick,70
85- Smith,14
73+ age,name
74+ 70,Rick
75+ 14,Smith
8676*/
8777const data = [
88- {
78+ {
8979 age : 70 ,
90- name : " Rick"
80+ name : ' Rick'
9181 } ,
9282 {
9383 age : 14 ,
94- name : "Smith"
95- } ,
96- ] ;
97- const columns : Column [ ] = [ "name" , "age" ] ;
98-
99- // we have to stringify the data with a row header
100- const dataString = await stringify ( data , columns )
101- writeCSV ( './examples/csv/names.csv' , dataString )
84+ name : 'Smith'
85+ }
86+ ]
87+ writeCSV ( './examples/csv/names.csv' , data )
10288
10389/*
10490Write one of the previously parsed csv examples
@@ -108,7 +94,5 @@ One,50,$0.5
10894Two,1.3,$10
10995Three,-0.3,$3000
11096*/
111- const data2 = parseColumnCSV as DataItem [ ] ; // have to recast the output
112- const columns2 : Column [ ] = [ "id" , "quantity" , "cost" ] ;
113- const dataString2 = await stringify ( data2 , columns2 )
114- writeCSV ( './examples/csv/prices-write.csv' , dataString2 )
97+ console . log ( parseColumnCSV )
98+ writeCSV ( './examples/csv/prices-write.csv' , parseColumnCSV )
0 commit comments