Skip to main content

CSV

CSV is a table written as plain text: one row per line, a separator between the cells, and usually a first line of column names. Every spreadsheet exports it, so it is the shortest way from a table of measurements, coordinates or prices to geometry. bitbybit.csv reads it and writes it.

Reading a table​

  • parseToArray gives every row as a list of cell texts. Nothing is converted to numbers.
  • parseToJson gives one object per row, keyed by the column names of the header row. numberColumns names the columns to read as numbers.
  • parseToJsonWithHeaders does the same for a file with no header line, with the names you give.
  • queryColumn lists the values of one column, found by its name. With asNumber on, they come back as numbers.
  • queryRowsByValue keeps the rows whose cell in a column equals a value.
  • getHeaders, getRowCount and getColumnCount describe the table.

Every reader takes the row and column separators, a line break and a comma unless you say otherwise. A semicolon is common in countries that write decimals with a comma. Written in a Rete or Blockly field, \n and \t are read as a line break and a tab, so a whole table fits in one text field.

A cell in double quotes may hold the separator, and a quote inside it is written twice.

A table into points​

Here a table holds the x, y and z of five points. Each column is read as numbers, the three columns are turned on their side into points with bitbybit.lists.flipLists, and one smooth curve passes through them:

Bitbybit Platform

A curve through points read from a CSV table in Rete

rete logoRete
Script Source (rete)
{
"id": "rete-v2-json",
"nodes": {
"table": {
"id": "table",
"name": "bitbybit.text.create",
"customName": "create",
"async": false,
"drawable": false,
"data": {
"genericNodeData": {
"hide": false,
"oneOnOne": false,
"flatten": 0,
"forceExecution": false
},
"text": "x,y,z\\n0,0,0\\n2,1.5,1\\n4,0.5,3\\n6,2.5,2\\n8,1,0"
},
"inputs": {},
"position": [
40,
40
]
},
"xs": {
"id": "xs",
"name": "bitbybit.csv.queryColumn",
"customName": "query column",
"async": false,
"drawable": false,
"data": {
"genericNodeData": {
"hide": false,
"oneOnOne": false,
"flatten": 0,
"forceExecution": false
},
"csv": "name,age\\nJohn,30\\nJane,25",
"column": "x",
"headerRow": 0,
"dataStartRow": 1,
"rowSeparator": "\\n",
"columnSeparator": ",",
"asNumber": true
},
"inputs": {
"csv": {
"connections": [
{
"node": "table",
"output": "result",
"data": {}
}
]
}
},
"position": [
460,
40
]
},
"ys": {
"id": "ys",
"name": "bitbybit.csv.queryColumn",
"customName": "query column",
"async": false,
"drawable": false,
"data": {
"genericNodeData": {
"hide": false,
"oneOnOne": false,
"flatten": 0,
"forceExecution": false
},
"csv": "name,age\\nJohn,30\\nJane,25",
"column": "y",
"headerRow": 0,
"dataStartRow": 1,
"rowSeparator": "\\n",
"columnSeparator": ",",
"asNumber": true
},
"inputs": {
"csv": {
"connections": [
{
"node": "table",
"output": "result",
"data": {}
}
]
}
},
"position": [
460,
520
]
},
"zs": {
"id": "zs",
"name": "bitbybit.csv.queryColumn",
"customName": "query column",
"async": false,
"drawable": false,
"data": {
"genericNodeData": {
"hide": false,
"oneOnOne": false,
"flatten": 0,
"forceExecution": false
},
"csv": "name,age\\nJohn,30\\nJane,25",
"column": "z",
"headerRow": 0,
"dataStartRow": 1,
"rowSeparator": "\\n",
"columnSeparator": ",",
"asNumber": true
},
"inputs": {
"csv": {
"connections": [
{
"node": "table",
"output": "result",
"data": {}
}
]
}
},
"position": [
460,
1000
]
},
"columns": {
"id": "columns",
"name": "bitbybit.lists.createList",
"customName": "create list",
"async": false,
"drawable": false,
"data": {},
"inputs": {
"listElements": {
"connections": [
{
"node": "xs",
"output": "result",
"data": {}
},
{
"node": "ys",
"output": "result",
"data": {}
},
{
"node": "zs",
"output": "result",
"data": {}
}
]
}
},
"position": [
880,
40
]
},
"points": {
"id": "points",
"name": "bitbybit.lists.flipLists",
"customName": "flip lists",
"async": false,
"drawable": false,
"data": {
"genericNodeData": {
"hide": false,
"oneOnOne": false,
"flatten": 0,
"forceExecution": false
},
"clone": true
},
"inputs": {
"list": {
"connections": [
{
"node": "columns",
"output": "list",
"data": {}
}
]
}
},
"position": [
1300,
40
]
},
"curve": {
"id": "curve",
"name": "bitbybit.occt.shapes.wire.interpolatePoints",
"customName": "interpolate points",
"async": true,
"drawable": true,
"data": {
"genericNodeData": {
"hide": false,
"oneOnOne": false,
"flatten": 0,
"forceExecution": false
},
"periodic": false,
"tolerance": 1e-7,
"parametrization": "chordLength"
},
"inputs": {
"points": {
"connections": [
{
"node": "points",
"output": "result",
"data": {}
}
]
}
},
"position": [
1720,
40
]
}
},
"modules": {}
}

In a project, keep the table as a file and load it with bitbybit.asset.getTextFile (see Assets). A changed file changes the model.

Writing a table​

  • arrayToCsv writes a list of rows, each a list of cells. A cell that holds a separator, a quote or a line break is quoted.
  • jsonToCsv writes a list of objects with the columns you name, in that order.
  • jsonToCsvAuto takes the columns from the first object's properties.

bitbybit.asset.download then saves the text as a .csv file, for a parts list or the measurements of a model.

Every function​

FunctionWhat it does
arrayToCsvWrites a list of rows, each a list of cells, as CSV text; a cell holding a separator, a quote or a line break is wrapped in double quotes.
getColumnCountCounts the cells of the first non-blank row, which is the number of columns; empty text gives 0.
getHeadersReads the cells of row headerRow as the header names; a row index past the end throws an error.
getRowCountCounts the data rows: all non-blank lines minus the ones before dataStartRow, or minus one header line when hasHeaders is true and dataStartRow is left out.
jsonToCsvWrites a list of objects as CSV text with the columns you name in headers, in that order; a property an object lacks becomes an empty cell.
jsonToCsvAutoWrites a list of objects as CSV text using the property names of the first object as the columns, in their order; an empty list gives empty text.
parseToArraySplits CSV text into a list of rows, each a list of cell strings; nothing is converted to numbers.
parseToJsonTurns CSV text into a list of objects, one per data row, keyed by the header names of row headerRow.
parseToJsonWithHeadersTurns CSV text into a list of objects keyed by the headers you give, for files without a header line; a header line the file does have is skipped by setting dataStartRow past it.
queryColumnLists every value of one column, found by its header name, in row order; a row without that cell gives an empty string.
queryRowsByValueKeeps only the rows whose cell in column equals value, giving them as objects keyed by the headers.