deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

Feature request: Webstream-based parser for CSV with headers

Open ayame113 opened this issue 2 years ago • 1 comments

I think we need a way to parse a CSV with headers while streaming the file contents. For example something like this:

Deno.openSync("path/to/csv").readable
  .pipeThrough(new CSVStream({ header: true }))
  .pipeTo(...)

Previously this was possible via the Deno.reader interface. Since the csv.ts parse function has become synchronous, there is no way to do this anymore. ( #2491 )

I think we need a new method based on Web Streams instead of the Deno.reader interface used previously.

prior art

csv-string, a CSV parser for Node.js, can parse a csv with headers and convert it to an array of objects.

const CSV = require('csv-string');
const parsedCsv = CSV.parse('a,b,c\n1,2,3\n4,5,6', { output: 'objects' });
console.log(parsedCsv);
// [
//   { a: '1', b: '2', c: '3' },
//   { a: '4', b: '5', c: '6' }
// ]

The python standard library also supports this.

import csv
with open('names.csv', newline='') as csvfile:
  reader = csv.DictReader(csvfile)
  for row in reader:
    print(row['first_name'], row['last_name'])

related: https://github.com/denoland/deno_std/issues/2291

ayame113 avatar Aug 24 '22 13:08 ayame113

I have an open PR that makes it possible to parse csv files with no headers. I'll make it work with the new stringify version this weekend, it's in #2168

luk3skyw4lker avatar Aug 25 '22 00:08 luk3skyw4lker

@luk3skyw4lker any updates?

lino-levan avatar Nov 01 '22 16:11 lino-levan

@lino-levan the PR is already merged, @kt3k @bartlomieju is this issue solved with the mentioned PR?

luk3skyw4lker avatar Nov 06 '22 23:11 luk3skyw4lker

There are currently two types of CSV parsers/stringify: WebStream-based and non-stream (converting strings). As of today, it should look like this:

string based web stream based
stringify
  • encoding/csv.ts
  • Can handle CSV with headers
N/A
parse
  • encoding/csv.ts
  • Can handle CSV with headers
  • encoding/csv/stream.ts
  • Cannot handle CSV with headers

What I'm proposing is to allow webstream based parsers to parse CSV with headers. So I think this issue should still be left open.


PS: I wonder if anyone needs a web stream based stringify?

ayame113 avatar Nov 07 '22 05:11 ayame113