Trouble importing csv data

Here is my python code (which runs in python fine);

import math as ma
import csv

brand ='phillips'
filename = 'C:\Users\gornall72\Desktop\import.csv'

with open(filename,'r') as file:
    reader = csv.DictReader(file)
    for row in reader:
        if(brand in row['Brand Name']):
            I = (float(row['dB']))

D= 3/ma.sqrt(30/I)

D2= D+3


print D2

When I try to bring it into Rhino I am getting a message saying “No module named csv”. The half of my Rhino code is:

import rhinoscriptsyntax as rs
import scriptcontext as sc
import math as ma

def main():

response=rs.MessageBox( "Evaluate Classroom?",  4)
if response !=7:
        
    layer = rs.GetLayer("Please select a layer")
    if layer:
        
        obj = rs.ObjectsByLayer(layer)

       
        rs.SelectObjects(obj)
        
        ObjId=rs.SelectedObjects(include_lights=False, include_grips=False)
        
        for obj in ObjId:
            cent=rs.SurfaceAreaCentroid(obj)
            print cent[0]
            rs.AddPoint(cent[0])
            
            Radius = 5
            
            rs.AddSphere(cent[0],Radius)

else:
    rs.MessageBox("Evaluation Aborted")

main()

Right now where radius is equal to 5 ( in the rhino python script) I am trying to get that equal to the value D2 found in my python formula(the first script) using the csv file. long term id like to automatically apply the information to the layers by a naming convention, maybe by making the brand = to the layer name, but solving this step first would help.

Thanks
–Chris

Hi Chris,

Rhino uses IronPython which is why you cannot import the csv module. Read here how to create your own reader just by parsing the file and splitting the read lines to obtain the required values for your script.

c.

1 Like

thank you!

Just as an FYI; I believe the csv module is working in the Rhino WIP.

Yes, I confirmed that the CSV module is available in Python in Rhino 6 WIP. Here is an example based on your code:

import math as ma
import csv
import rhinoscriptsyntax as rs

def CSVbrand():
    brand ='phillips'
    #prompt the user for a file to import
    filter = "CSV file (*.csv)|*.csv|All Files (*.*)|*.*||"
    filename = rs.OpenFileName("Open Point File", filter)
    if not filename: return

    with open(filename,'r') as file:
        reader = csv.DictReader(file)
        for row in reader:
            if(brand in row['Brand Name']):
                I = (float(row['dB']))

    D= 3/ma.sqrt(30/I)

    D2= D+3


    print D2

##########################################################################
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == "__main__" ):
    CSVbrand()

I have found a better way in the meantime. Just use the .Net method. It works perfectly.

import rhinoscriptsyntax as rs
import Rhino
import clr
clr.AddReference("System")
from System.IO import *

lines = File.ReadLines("C:\Python\data4.csv")
for line in lines:
...

@onrender,

one difference is that the .NET method will not work on Mac. Parsing it as a text file works on all platforms (Win/Mac) and versions (V5, V6) of Rhino.

_
c.

There are many ways to use the CSV reader. This one runs on both Mac and Windows. Of course it is Rhino 6 only. The CSV is attached. This one breaks the CSV down into a dictionary.

import math as ma
import csv
import rhinoscriptsyntax as rs


def CSVbuilding():
    #prompt the user for a file to import
    filter = "CSV file (*.csv)|*.csv|*.txt|All Files (*.*)|*.*||"
    filename = rs.OpenFileName("Open Point File", filter)
    if not filename: return

    with open(filename) as csvfile:
        reader = csv.DictReader(csvfile)
        for row in reader:
            print(row['Use'], row['Square Footage'])

##########################################################################
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == "__main__" ):
    CSVbuilding()

There is a more extensive guide on the CSV module in Rhino.Python here: http://developer.rhino3d.com/guides/rhinopython/python-csv-file/#reading-into-a-list

buildingdata.zip (381 Bytes)

It won’t? I know the path is not going to work, but other than that the script looks like it would work just fine.

Just for fun, the CSV module also allows a read that makes a list of each row:

import math as ma
import csv
import rhinoscriptsyntax as rs


def CSVlist():
    #prompt the user for a file to import
    filter = "CSV file (*.csv)|*.csv|*.txt|All Files (*.*)|*.*||"
    filename = rs.OpenFileName("Open Point File", filter)
    if not filename: return

    with open(filename) as csvfile:
        reader = csv.reader(csvfile)
        for row in reader:
            print row

##########################################################################
# Check to see if this file is being executed as the "main" python
# script instead of being used as a module by some other python script
# This allows us to use the module which ever way we want.
if( __name__ == "__main__" ):
    CSVlist()

There is a more extensive guide on the CSV module in Rhino.Python here: http://developer.rhino3d.com/guides/rhinopython/python-csv-file/#reading-into-a-list

@stevebaer, i’ve been reported some problems when my scripts where importing System and in particular Windows.Forms. Do you say i can use all under System even on a Mac ?

_
c.

System.Windows.Forms is a separate assembly which is very Windows specific. System on the other hand should completely work on Mac.

I can confirm that System.Drawing.Color also works. Is there a page where i can look up what is supported ?

_
c.

Basically anything under System.Windows is where you are going to run into problems on Mac. i.e. if you see the word Windows in your script, you may run into dragons on Mac :slight_smile:

OK thanks, i’ll take care and watch out for dragons then :wink:

_
c.

4 posts were split to a new topic: Eto user interface with python

A post was merged into an existing topic: Eto user interface with python

I am not sure if you are looking for a Eto GridView control:

image

The GridView control is very flexible. This is some very basic code to create the control above:

        #Create Gridview sometimes called a ListView
        self.m_gridview = forms.GridView()
#        self.m_gridview.Size = drawing.Size(400, 400)
        self.m_gridview.ShowHeader = True
        self.m_gridview.DataStore = (['first pick', 'second pick', 'third pick'],['second','fourth','last'])
        column1 = forms.GridColumn()
        column1.HeaderText = 'Column 1'
        column1.DataCell = forms.TextBoxCell(0)
        self.m_gridview.Columns.Add(column1)
        column2 = forms.GridColumn()
        column2.HeaderText = 'Column 2'
        column2.DataCell = forms.TextBoxCell(1)
        self.m_gridview.Columns.Add(column2)
        column3 = forms.GridColumn()
        column3.HeaderText = 'Column 3'
        column3.DataCell = forms.TextBoxCell(2)
        self.m_gridview.Columns.Add(column3)

Or is it a more ListBox?

        #Create ListBox
        self.m_listbox = forms.ListBox()
        self.m_listbox.DataStore = ['first pick', 'second pick', 'third pick']
        self.m_listbox.SelectedIndex = 1
1 Like

@scottd, thanks for the example. It is Eto.Forms.GridView which seems to be closest to Windows.Forms.ListView.

_
c.