# Perspective onFileReceived - CSV

**URL:** <https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893>\
**Category:** Ignition\
**Created:** [October 21, 2020, 7:41pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893 "2020-10-21T19:41:57Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![DSI600](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@DSI600](https://forum.inductiveautomation.com/u/DSI600)\
**Post date:** [October 21, 2020, 7:41pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/1 "2020-10-21T19:41:57Z")

</div>

Having issues trying to figure out how to parse a CSV and get it into a database from perspective using the file upload component. I have looked around most of the afternoon on the forum and can’t quite find any examples that are not people trying to parse images.

```auto
	sdata = event.file.getBytes()
	byteArray = bytearray(sdata).decode("UTF-8")
	system.perspective.print(byteArray)

```

I have the above and it will print out the data in the CSV file in a string list I am assuming with the decode. What I need to do though is get the data into ‘rows’ so I can insert each row from the csv to the database. I had this working in Vision with open.file (below) but can’t seem to figure out how to migrate this to perspective.

```auto
	filePath = system.file.openFile("csv", "%user%")
	csvFile = open(filePath, 'r')
	reader = csv.DictReader(csvFile)
		
	for row in reader:
	        entrytype = row['entrytype']
		freq= row['freq']
		taskName = row['taskname']
		priority = row['priority']
		system.db.runUpdateQuery("INSERT INTO maint_admin(taskname,freq,entrytype,priority) VALUES ('%s', '%s', '%s', %d)" % (taskName, freq, entrytype, int(priority)))

```

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [October 21, 2020, 8:29pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/2 "2020-10-21T20:29:49Z")

</div>

If you want to use Python’s built-in `csv` module, you need to construct a file-like object to pass to the reader. Luckily, Python also predicted the idea of interpreting a string in memory as a ‘file-like’ object, so the `StringIO` module already exists.  
What you want is probably something like this:

```auto
from StringIO import StringIO

fileContents = event.file.getString("UTF-8")
reader = csv.DictReader(StringIO(fileContents))

for row in reader:
	print row

```

---

<div class="post-metadata">

**Author:** ![DSI600](https://avatars.discourse-cdn.com/v4/letter/d/ad7895/32.png) [@DSI600](https://forum.inductiveautomation.com/u/DSI600)\
**Post date:** [October 21, 2020, 8:56pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/3 "2020-10-21T20:56:17Z")

</div>

Awesome - Felt I was close just couldn’t find what I needed. Should have asked sooner 🙂

FYI for future readers below was the updated script to make it work from perspective

```auto
	from StringIO import StringIO
	import csv
	fileContents = event.file.getString("UTF-8")
	reader = csv.DictReader(StringIO(fileContents))
	for row in reader:
		system.perspective.print(row)

```

Thanks again!

---

<div class="post-metadata">

**Author:** ![HALW80](https://avatars.discourse-cdn.com/v4/letter/h/47e85d/32.png) [@HALW80](https://forum.inductiveautomation.com/u/HALW80)\
**Post date:** [December 11, 2020, 12:21pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/4 "2020-12-11T12:21:56Z")

</div>

Thanks DS1600 that is most helpful

---

<div class="post-metadata">

**Author:** ![ntheuer](https://avatars.discourse-cdn.com/v4/letter/n/ccd318/32.png) [@ntheuer](https://forum.inductiveautomation.com/u/ntheuer)\
**Post date:** [April 12, 2022, 9:12pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/5 "2022-04-12T21:12:42Z")

</div>

If using the parse to string method, what is the equivalent of this next function? I need the system to disregard the first row in the .csv if possible. (I would like to avoid having the user have to modify any file)

Thanks

![image](https://us1.discourse-cdn.com/inductiveautomation/original/3X/2/d/2d4943312608d428699c786759a78dbad7fdb964.png)

---

<div class="post-metadata">

**Author:** ![josborn](https://avatars.discourse-cdn.com/v4/letter/j/4bbf92/32.png) [@josborn](https://forum.inductiveautomation.com/u/josborn)\
**Post date:** [April 12, 2022, 9:49pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/6 "2022-04-12T21:49:48Z")

</div>

Please use preformatted code `</>` instead of a picture of code.  
What do you think the next function is doing here? Why not just ignore the first row?

```auto
from StringIO import StringIO
import csv
	
fileContents = event.file.getString("UTF-8")
reader = csv.DictReader(StringIO(fileContents))
	
for row in reader:
	if row >0:
		system.perspective.print(row)

```

---

<div class="post-metadata">

**Author:** ![JordanCClark](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jordancclark/32/83362_2.png) [@JordanCClark](https://forum.inductiveautomation.com/u/JordanCClark)\
**Post date:** [April 13, 2022, 12:56pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/7 "2022-04-13T12:56:12Z")

</div>

Are you ignoring the first line of data, or are you ignoring it because it’s a header line? The csv module has different readers, so it helps to know what they do.

```python
from StringIO import StringIO
import csv

csvString = """col1, col2, col3
abc, def, ghi
jkl, mno, pqr
stu, vwx, yz"""

reader = csv.DictReader(StringIO(csvString))

print 'Using csv.DictReader()'
for row in reader:
		print row

print '\nUsing csv.reader()'
reader2 = csv.reader(StringIO(csvString))
for row in reader2:
	print row

```

Output:

```nohighlight
Using csv.DictReader()
{' col3': ' ghi', ' col2': ' def', 'col1': 'abc'}
{' col3': ' pqr', ' col2': ' mno', 'col1': 'jkl'}
{' col3': ' yz', ' col2': ' vwx', 'col1': 'stu'}

Using csv.reader()
['col1', ' col2', ' col3']
['abc', ' def', ' ghi']
['jkl', ' mno', ' pqr']
['stu', ' vwx', ' yz']
>>> 

```

---

<div class="post-metadata">

**Author:** ![Andrea\_Rector](https://avatars.discourse-cdn.com/v4/letter/a/82dd89/32.png) [@Andrea\_Rector](https://forum.inductiveautomation.com/u/Andrea_Rector)\
**Post date:** [July 6, 2022, 9:09pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/8 "2022-07-06T21:09:41Z")

</div>

> [@josborn](#):
>
> ```auto
> for row in reader:
> if row >0:
> system.perspective.print(row)
> 
> ```

I tried this code but am getting the following error:

Error running action 'component.onFileReceived' on MainPages/AccountingSettings@D/root/FlexContainer\_3/FileUpload: Traceback (most recent call last): File "function:runAction", line 6, in runAction  
java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

---

<div class="post-metadata">

**Author:** ![danieltalbot](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/danieltalbot/32/11201_2.png) [@danieltalbot](https://forum.inductiveautomation.com/u/danieltalbot)\
**Post date:** [September 12, 2022, 5:37pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/9 "2022-09-12T17:37:31Z")

</div>

I’m getting this as well

---

<div class="post-metadata">

**Author:** ![Transistor](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/transistor/32/3380_2.png) [@Transistor](https://forum.inductiveautomation.com/u/Transistor)\
**Post date:** [September 12, 2022, 6:35pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/10 "2022-09-12T18:35:02Z")

</div>

> `if row >0:`

`row` is not an integer so you can't use the `>` comparator.

- In Jordan's first example it's a dictionary. e.g.,  
`{' col3': ' ghi', ' col2': ' def', 'col1': 'abc'}`
- In the second example it's a list. e.g.,  
`['col1', ' col2', ' col3']`

Use `print type(row)` to see for yourself.

---

<div class="post-metadata">

**Author:** ![justinedwards.jle](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/justinedwards.jle/32/37854_2.png) [@justinedwards.jle](https://forum.inductiveautomation.com/u/justinedwards.jle)\
**Post date:** [September 12, 2022, 6:59pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/11 "2022-09-12T18:59:53Z")

</div>

Perhaps this would render the result you are looking for:

```auto
rowNum = 0
for row in reader:
	rowNum = rowNum + 1
	if rowNum > 1
		print row

```

---

<div class="post-metadata">

**Author:** ![jlandwerlen](https://avatars.discourse-cdn.com/v4/letter/j/bc8723/32.png) [@jlandwerlen](https://forum.inductiveautomation.com/u/jlandwerlen)\
**Post date:** [September 12, 2022, 8:11pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/12 "2022-09-12T20:11:45Z")

</div>

Probably more pythonic to use enumeration, something like

```auto
for x, row in enumerate(reader):

```

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [September 12, 2022, 8:40pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/13 "2022-09-12T20:40:14Z")

</div>

If using the `csv.DictReader()` option. Might be better to do:

```python
for row in reader:
    for name,val in row.iteritems():
          print name,':', val

```

---

<div class="post-metadata">

**Author:** ![jacque\_trahan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jacque_trahan/32/58886_2.png) [@jacque\_trahan](https://forum.inductiveautomation.com/u/jacque_trahan)\
**Post date:** [March 20, 2025, 7:08pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/14 "2025-03-20T19:08:09Z")

</div>

Was the csv function updated today to take of "/" on uploads?

All of a sudden my fullpath on a csv file upload has all the "/" taken out of it which started today:

> getFileContents = event.file.getString()

Will turn paths  
from [provider]folder1/folder2/folder3  
to  
[provider]folder1folder2folder3

---

<div class="post-metadata">

**Author:** ![lrose](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/lrose/32/62724_2.png) [@lrose](https://forum.inductiveautomation.com/u/lrose)\
**Post date:** [March 20, 2025, 7:12pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/15 "2025-03-20T19:12:12Z")

</div>

> [@jacque\_trahan](#):
>
> Was the csv function updated today

Did you perform an update?

---

<div class="post-metadata">

**Author:** ![jacque\_trahan](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/jacque_trahan/32/58886_2.png) [@jacque\_trahan](https://forum.inductiveautomation.com/u/jacque_trahan)\
**Post date:** [March 20, 2025, 7:15pm UTC](https://forum.inductiveautomation.com/t/perspective-onfilereceived-csv/39893/16 "2025-03-20T19:15:43Z")

</div>

I was just about to reply back. There was a mishap on which type of CSV we save the file as.

Think UTF-8 was chosen but once we save it as a normal csv everything worked fine. There was "data fire" going on on and this happened right in the middle of it.
