Read CSV file by python

HI, I am trying to read data of csv file by using python but It’s not working with me.

this my code on python script.

import clr
clr.AddReference(‘ProtoGeometry’)
from Autodesk.DesignScript.Geometry import *

clr.AddReference(‘RevitNodes’)
from System.IO import FileInfo

import sys
sys.path.append(“C:\Program Files (x86)\IronPython 2.7\Lib”)

import csv
from System.IO import FileInfo

#code
file = FileInfo(IN[0])

cr = csv.reader(response)
data =
for cr in file:
for row in cr:
data.append(row)

Assign your output to the OUT variable.

OUT = data

@Deniz_Maral @dzarur @d.sellam Can you help me to solve this?

when I use this code the out = path but I want get the data as string no just the path

import sys
sys.path.append(‘C:\Program Files (x86)\IronPython 2.7\Lib’)
import csv
import StringIO

csv_in = IN[0]
data =

source = StringIO.StringIO(csv_in)
csv_out = csv.reader(source, dialect=csv.excel)
for row in csv_out:
data.append(row)

OUT = data

use r‘C:\Program Files (x86)\IronPython 2.7\Lib’

What data are you talking about?

the same

Why do you need Python for that? You can use Data.ImportCSV node to get data.

You need to open the file first then use the CSV.reader line.

with open(inputfile) as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
        output.append(row)

You could just read it as a text file in Dynamo then use a string split node to split by “,” as a secondary option

1 Like

no I need it from python

maybe a problem with the inputfile? we need get the path of file (not file), don’t forget the raw string if necessary
or a problem with file’s permission?

What says the error message?
Can you try to open(inputfile[0])?

ok , now when I use it’s working but I have one problem, that is when I make update in csv file there is no change in dynamo I need to double check on node input to refresh … can i add something to the code to make it refresh automatic or maybe I should make input file as file from path not just file.

with open(inputfile) as csv_file:
csv_reader = csv.reader(csv_file, delimiter=’,’)
for row in csv_reader:
output.append(row)

It’s working but not updated please read the last reply from me

I will explain my problem to you deniz that in dynamo nodes when I use import\read excel or read from text node I can add file from path as a input but when I use import csv I can’t add file from path as it needs file directly so when I make edit on csv file there is no change in list

@MAIA17112
if your csv file is updated you need relaunch the script or use the Periodic Run Mode

path = IN[0]
z=[]



with open(path) as csv_file:
	csv_reader = csv_file.readlines()
	for row in csv_reader:
		z.append(row)
			

OUT = z
1 Like

Do like I did. Use Passtrough node from Clockwork and it will work.

yes thanks deniz it’s a perfect solution but I have a question… how I can make list “II” like list “I”

You can use String.TrimWhiteSpace node.

or in Python

z.append(row.strip())

result is null!

path = IN[0]
z=[]



with open(path) as csv_file:
	csv_reader = csv_file.readlines()
	for row in csv_reader:
		z.append(row.strip())
			

OUT = z
1 Like