Import, read out and visualize CSV file

Hello, I have a CSV file and i want to import the file and read the data out and visualize the Temp-Values on a dashboard. Can anyone tell me how its possible to do this or how its possible to connect the csv to a device?

The CSV is in the following format:

(column 1) Date: (column 2) Temp-value:
1577837700 6,1

Thank You :slight_smile:

Hi @lucky-luc1,

You will need a library to convert CSV from JSON, I recommend “csvtojson”. Its pretty simple to be honest, try do a research on Stack Overflow and you will find that. Two more things, I recommend you use lower case and non special character on column headers (e.g. date and tempvalue) it will make your life easier when trying to access those variables. And below a code example, copy and paste into your code:

const csv = require("csvtojson");
const csvFilePath = "./demand_by_timestamp.csv";

//notice that "datas" has the CSV in JSON format already.
const datas = await csv().fromFile(csvFilePath).subscribe();

for (const data of datas) {

   console.log(data.date)
  }

Hope that helps.
-Guilherme

2 Likes

Thank you! I will try it :slight_smile:

1 Like

Hi @ guilhermeco

I have tried your suggestion to convert a csv file to json but have picked up an error can you please help me to resolve it.

This is my code:

const { Analysis } = require("@tago-io/sdk");

async function myAnalysis(context, scope) {

const csv = require(“csvtojson”);

const csvFilePath = “path to csv file”

//Get the csv data and insert into discharge as JSON

const discharge = await csv().fromFile(csvFilePath).subscribe();

for (const data of discharge) {

context.log(data.discharge)

}

}

module.exports = new Analysis(myAnalysis);

This is the error:

[2021-09-03 11:55:50] Error: Cannot find module ‘csvtojson’ Require stack: - /var/task/runAnalysis.js - /var/task/index.js - /var/runtime/UserFunction.js - /var/runtime/index.js at Function.Module._resolveFilename (internal/modules/cjs/loader.js:889:15) at Function.Module._load (internal/modules/cjs/loader.js:745:27) at Module.require (internal/modules/cjs/loader.js:961:19) at require (internal/modules/cjs/helpers.js:92:18) at Analysis.myAnalysis [as analysis] (eval at ____exec (/var/task/runAnalysis.js:12:5), :17:13) at Analysis.run (/var/task/node_modules/@tago-io/sdk/out/modules/Analysis/Analysis.js:75:18) at ____exec (/var/task/runAnalysis.js:12:42) at module.exports (/var/task/runAnalysis.js:21:39) at Gunzip.cb (/var/task/index.js:60:9) at Gunzip.zlibBufferOnEnd (zlib.js:159:10) at Gunzip.emit (events.js:400:28) at endReadableNT (internal/streams/readable.js:1317:12) at processTicksAndRejections (internal/process/task_queues.js:82:21)

[2021-09-03 11:55:50] Starting analysis 6131d79406ba7900125714ce

Appreciate the help.

Kind Regards
Flaxie

Hi @flaxies ,

that example I’ve provided is to run externally.

In order to run internally you will need to:

1- install tago-builder (tago-builder - npm)

npm install -g tago-builder

2- open the script on VScode (or any other tool) and run

tago-builder myanalysis.js myanalysis.tago.js

where the first “myanalysis.js” is the path for your script and the second one “myanalysis.tago.js” is the new script name
3- It will generate a new script file, copy and paste it, or import it on TagoIO.

Let me know if you have any further questions.

-Guilherme

Hi @guilhermeco

I am receiving the following error when i try to build. Any idea what could be the problem.

PS C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project> tago-builder C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project\ReadCSVfileAnalysis.js
TagoIO (https://tago.io/)
TagoIO Builder V2.4.6 (https://git.io/JJ8Si)

Generated by     :: Dell
Generated at     :: 2021-09-04T15:28:25.671Z - UTC
Machine          :: Warriors - win32
Origin File      :: C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project\ReadCSVfileAnalysis.js
Destination File :: C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project\ReadCSVfileAnalysis.tago-io.js

Building... -It may take a while-
Entry module not found: Error: Can't resolve './C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project\ReadCSVfileAnalysis.js' in 'C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project'
PS C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project> 

Hi @guilhermeco

If i remove the path as VS is already at the right location i get this error:

Building... -It may take a while-
Module not found: Error: Can't resolve 'csvtojson' in 'C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project'        
PS C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project> tago-builder ReadCSVfileAnalysis.js ReadCSVfileAnalysis.tago.js
TagoIO (https://tago.io/)
TagoIO Builder V2.4.6 (https://git.io/JJ8Si)

Generated by     :: Dell
Generated at     :: 2021-09-04T16:58:12.956Z - UTC
Machine          :: Warriors - win32
Origin File      :: ReadCSVfileAnalysis.js
Destination File :: ReadCSVfileAnalysis.tago.js

Building... -It may take a while-
Module not found: Error: Can't resolve 'csvtojson' in 'C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project'        
PS C:\Users\Dell\Desktop\Shepherd_Tech_Project\Water_Canel_Project> 

Hi @guilhermeco

Sorry no worries …did not realize i had to install csvtojson first. :grinning:

npm i csvtojson

Ok so “tago-builder ReadCSVfileAnalysis.js” built a new file called “ReadCSVfileAnalysis.tago-io.js”.
I copied and pasted this into TagoIO and ran it.
I now get the error:

[2021-09-05 09:08:32] Error: File does not exist. Check to make sure the file path to your csv is correct. at eval (webpack:///./node_modules/csvtojson/v2/Converter.js?:81:37) at suppressedCallback (fs.js:236:5) at FSReqCallback.oncomplete (fs.js:180:23)
[2021-09-05 09:08:31] Starting analysis 6131d79406ba7900125714ce

I have confirmed the path is correct.

1 Like

Hi @flaxies,

You can simply copy and paste the content then, if you’re having problem by selecting the file. It should work though.

1 Like