# CSV file to 2D array

**URL:** <https://forum.dynamobim.com/t/csv-file-to-2d-array/20949>\
**Category:** Share\
**Tags:** csv\
**Created:** [April 11, 2018, 4:27pm UTC](https://forum.dynamobim.com/t/csv-file-to-2d-array/20949 "2018-04-11T16:27:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![cgartland](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cgartland/32/45378_2.png) [@cgartland](https://forum.dynamobim.com/u/cgartland)\
**Post date:** [April 11, 2018, 4:27pm UTC](https://forum.dynamobim.com/t/csv-file-to-2d-array/20949/1 "2018-04-11T16:27:02Z")

</div>

After becoming somewhat frustrated with Dynamo’s native CSV.ReadFromFile node, I decided to find a smarter way to parse a CSV file. Any and all commas will cause Dynamo to split data into a new column, which becomes problematic when you have commas within a cell.

More specifically, the data I am using is downloaded as a CSV file from Google Sheets. By reading the file as text and instead parsing it with Python, the structure of the data is maintained. Above is the output from the native Dynamo node and below is what is returned from the Python script:

 ![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/c/2/c231e2db796c8f9b083be1926865b0223c3db725.png)

The Python script itself is very simple (taken in large part from [this post](https://stackoverflow.com/questions/14999528/python-how-to-read-parse-csv-like-line/14999585?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa) on Stack Overflow and adapted to work with IronPython in Dynamo)

* * *

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

myCSV = IN[0]  
output =

source = StringIO.StringIO(myCSV)  
csvOUT = csv.reader(source , dialect=csv.excel)

for line in csvOUT:  
output.append(line)

OUT = output

* * *

EDIT: After looking at the output a bit more closely, neither solution seems to handle extra quotes very well. The item testB2" has two extra quotes in the native Dynamo output and one extra quote in Python output. Other than that, it works pretty well.

EDIT 2: The CSV dialect was actually incorrect and should instead be the csv.excel dialect. Otherwise, it parses the file incorrectly.

---

<div class="post-metadata">

**Author:** ![cgartland](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cgartland/32/45378_2.png) [@cgartland](https://forum.dynamobim.com/u/cgartland)\
**Post date:** [April 11, 2018, 4:53pm UTC](https://forum.dynamobim.com/t/csv-file-to-2d-array/20949/2 "2018-04-11T16:53:40Z")

</div>

Data used as a test case:

![image](https://us1.discourse-cdn.com/flex022/uploads/dynamobim/original/3X/d/b/db4773995144d5f34295a5e6873305c05246f0ee.png)

---

<div class="post-metadata">

**Author:** ![jacob.small](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/jacob.small/32/161030_2.png) [@jacob.small](https://forum.dynamobim.com/u/jacob.small)\
**Post date:** [April 11, 2018, 6:23pm UTC](https://forum.dynamobim.com/t/csv-file-to-2d-array/20949/3 "2018-04-11T18:23:40Z")

</div>

Reading the file as text certainly helps. Also bouncing though xls can save some headaches.

---

<div class="post-metadata">

**Author:** ![cgartland](https://sea2.discourse-cdn.com/flex022/user_avatar/forum.dynamobim.com/cgartland/32/45378_2.png) [@cgartland](https://forum.dynamobim.com/u/cgartland)\
**Post date:** [April 11, 2018, 6:38pm UTC](https://forum.dynamobim.com/t/csv-file-to-2d-array/20949/4 "2018-04-11T18:38:09Z")

</div>

Yes, reading from an excel file is definitely the most reliable way to import structured data (and usually easier than working with CSV files)
