Hello everybody
Tell me, can anyone have a node code for a grasshopper to read a csv file and then write data from this file, for example, to List<>?
A csv file has multiple rows of data, with each row having multiple values. So a List<T>
can’t really store that complexity, unless each row encodes a single data item such as a point or a colour.
Can you be more explicit about what the data in your csv file represents.
Thank you very much for your answer, while I’m just interested in importing the data of a simple table by row-divided into rows.
the goal of all is to create a node that will take data from the table, read data from the csv file and output it to the grasshopper
for example, we read this csv file, and set the node to the value B20 and it displays the data associated with this index, in this case B20 is d 4
I hope I explained clearly
maybe someone already did something like that?
or can this problem be solved in a simpler way?
read in gh.csv (99 Bytes)
If you don’t really need to develop a component…
(1) If your data needs to be organized the way as in your picture, use LunchBox’s Read CSV
.
And Create CSV
to create such a file.
lunchbox_csv.gh (6.5 KB)
(2) If your data is organized transposed and with a header (which is probably more natural), such as:
Name,Data1,Data2
B10,1,a
B12,2,b
B15,3,c
B20,4,d
B25,5,e
B30,6,f
B40,7,g
B50,8,h
B60,9,i
You may use Pancake for its CSV read/write support.
pancake_csv.gh (9.6 KB)
csv extractor.gh (17.1 KB)
Guys thank you very much
and it is possible to make all this as in c sharp in the form of one node?
can there be any example of how to correctly read the csv file and then output data on the parameter?
using System;
using System.Collections;
using System.Collections.Generic;
using Rhino;
using Rhino.Geometry;
using Grasshopper;
using Grasshopper.Kernel;
using Grasshopper.Kernel.Data;
using Grasshopper.Kernel.Types;
using System.IO;
/// <summary>
/// This class will be instantiated on demand by the Script component.
/// </summary>
public class Script_Instance : GH_ScriptInstance
{
#region Utility functions
/// <summary>Print a String to the [Out] Parameter of the Script component.</summary>
/// <param name="text">String to print.</param>
private void Print(string text) { /* Implementation hidden. */ }
/// <summary>Print a formatted String to the [Out] Parameter of the Script component.</summary>
/// <param name="format">String format.</param>
/// <param name="args">Formatting parameters.</param>
private void Print(string format, params object[] args) { /* Implementation hidden. */ }
/// <summary>Print useful information about an object instance to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj) { /* Implementation hidden. */ }
/// <summary>Print the signatures of all the overloads of a specific method to the [Out] Parameter of the Script component. </summary>
/// <param name="obj">Object instance to parse.</param>
private void Reflect(object obj, string method_name) { /* Implementation hidden. */ }
#endregion
#region Members
/// <summary>Gets the current Rhino document.</summary>
private readonly RhinoDoc RhinoDocument;
/// <summary>Gets the Grasshopper document that owns this script.</summary>
private readonly GH_Document GrasshopperDocument;
/// <summary>Gets the Grasshopper script component that owns this script.</summary>
private readonly IGH_Component Component;
/// <summary>
/// Gets the current iteration count. The first call to RunScript() is associated with Iteration==0.
/// Any subsequent call within the same solution will increment the Iteration count.
/// </summary>
private readonly int Iteration;
#endregion
/// <summary>
/// This procedure contains the user code. Input parameters are provided as regular arguments,
/// Output parameters as ref arguments. You don't have to assign output parameters,
/// they will have a default value.
/// </summary>
private void RunScript(string file, string key, ref object data)
{
var csv = File.ReadAllLines(file);
if(csv.Length == 0)
return;
var keyList = csv[0].Split(',');
var index = -1;
for(var i = 0;i < keyList.Length;i++)
{
if(keyList[i].Trim() == key)
{
index = i;
break;
}
}
if(index == -1)
return;
var list = new List<string>(csv.Length - 1);
for(var i = 1;i < csv.Length;i++)
{
var dataRow = csv[i].Split(',');
if(dataRow.Length <= index){
list.Add(null);
}else{
list.Add(dataRow[index].Trim());
}
}
data = list;
}
// <Custom additional code>
// </Custom additional code>
}
Thank you so much!!!
I’ll analyze your code now, I need to understand how it works !!
Rays of gratitude !!! Thank you very much!
I have to dig this up for a follow-up question.
How do I reverse the process? How to generate a .csv with different columns?
filename = "test.csv";
filepath = "C:/.../path/"+filename;
List<String> csv = new List<String>();
csv.Add("Header 1,Header2,Header3,Header4");
csv.Add("Input1,Input2,Input3,Input4"); //Add Row 1
csv.Add("Input1,Input2,Input3,Input4");//Add Row 2
System.IO.File.WriteAllLines(filepath,csv,System.Text.Encoding.UTF8);
This does only give me single cells with all values.
Adding string[…] or Additional Lists into the csv-List did not help so far.
Worked like a charm.
Thanks!
And I searched for a Solution in the methods instead of just using the csv correctly.