deno_std
deno_std copied to clipboard
Feature request: Webstream-based parser for CSV with headers
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
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 any updates?
@lino-levan the PR is already merged, @kt3k @bartlomieju is this issue solved with the mentioned PR?
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 |
|
N/A |
parse |
|
|
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?