FileAttachment now supports CSV (and TSV)

We’ve just shipped a change that makes it a little easier to read comma-separated values (CSV) in Observable:

data = FileAttachment("example.csv").csv()

No parser required!

The built-in CSV parser also supports automatic type inference if you set typed = true:

data = FileAttachment("example.csv").csv({typed: true})

If you don’t want to treat the first row as a header row of column names, you can set array = true:

data = FileAttachment("example.csv").csv({array: true})

There’s a file.tsv() method for tab-separated values, too.

We’ve updated the File Attachments notebook accordingly, and we’re in the process of updating notebooks to demonstrate this new feature. Hope you find it useful!

8 Likes

Thanks Mike! I’m curious to know how you’re updating example notebooks following a change such as this, or the move to D3 6.0. Is there any bulk editing functionality in the works? :thinking:

No bulk editing yet, but I definitely want it!

4 Likes

<looks at Mike’s published notebook count>

Huh, I have no idea why you’d be into that :stuck_out_tongue:

3 Likes

Thanks Mike! You’re killing it!

1 Like

Is there a way to have more control on the types passed?
My CSV data has a date that is just a year as a string and using {typed: true} it turns it into a number, which then causes formatting issues when used with Table

I am hoping to find out the I can do something like:

nitrateData = FileAttachment("nitrate_leaching.csv").csv({types: string , string,  string, date, number     })

input data example below:

Nitrate[0]: Object {
  geography_type: "Region"
  geography_name: "Auckland"
  animal: "Beef cattle"
  year: "1990"
  no3_kg_per_yr: "3152572.892"
}
1 Like

Not really an answer, but you can “reconvert” the date field like this:

FileAttachment("nitrate_leaching.csv")
  .csv({ typed: true })
  .then(data => (data.forEach(d => (d.year = new Date(d.year, 0))), data))
2 Likes

I recommend using UTC (new Date(Date.UTC(d.year, 0, 1))), but yes, and thank you for replying @Fil!

2 Likes