Last active
February 19, 2018 16:57
-
-
Save IsmiKin/7457de4e4d24fe83328fcf8ccd48a9c9 to your computer and use it in GitHub Desktop.
Tableschema test playground
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const {Table} = require('tableschema') | |
const content = [['date_column', 'integer_column', 'string_column'], | |
['01-01-2017', '100', 'lala'], | |
['01-01-2017', '200', 'coco'], | |
['01-01-2018', '300', 'cucu'], | |
['01-01-2017', '200', 'wakaka'] | |
] | |
// Linux ("\n"), Apple ("\r") and Windows ("\r\n") row delimiters. | |
// ltrim, rtrim, trim -> ignore whitespace immediately delimiter | |
// auto_parse --> the parser will attempt to convert input string to native type | |
const csvParserOptions = { | |
delimiter: ';', | |
to: 5, | |
relax_column_count: true | |
} | |
// const csvParserOptions = { | |
// delimiter: ';', | |
// escape: '\\', | |
// rowDelimiter: '\n', | |
// skip_empty_lines: true, | |
// to: 100 | |
// } | |
// | |
Table.load('data.csv', csvParserOptions) | |
.catch(function (errors) { | |
console.log('----- ERRORS') | |
console.log(errors) | |
}) | |
.then(function (table) { | |
return table.infer() // infer a schema | |
}) | |
.then(function (schema) { | |
console.log('-------- DATA.CSV') | |
console.log(schema) | |
console.log() | |
return schema | |
}) | |
Table.load(content).then(function (table) { | |
return table.infer() // infer a schema | |
}) | |
.then(function (schema) { | |
console.log('-------- ARRAY CONTENT') | |
console.log(schema) | |
return schema | |
}) | |
// await table.read({keyed: true}) // read the data | |
// await table.schema.save() // save the schema | |
// await table.save() // save the data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const fs = require('fs') | |
const parse = require('csv-parse') | |
const tableschema = require('tableschema') | |
let schema | |
// read CSV file | |
fs.readFile('data.csv', (err, data) => { | |
parse(data, (error, values) => { | |
const headers = values.shift() | |
infer_schema(headers, values) | |
// save JSON into file | |
// do something after saving | |
}) | |
}) | |
function write_schema (schema) { | |
console.log("schema", schema); | |
fs.writeFile('schema.json', schema, (e) => { | |
if (e) { | |
console.log(e) | |
} | |
}) | |
} | |
async function infer_schema (headers, values) { | |
let schema = await tableschema.infer(headers, values) | |
console.log(); | |
write_schema(schema) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment