# How do i import CSV file into Table

**URL:** <https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830>\
**Category:** Ignition\
**Tags:** ignition80\
**Created:** [May 18, 2020, 9:05am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830 "2020-05-18T09:05:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Callmesoon](https://avatars.discourse-cdn.com/v4/letter/c/c67d28/32.png) [@Callmesoon](https://forum.inductiveautomation.com/u/Callmesoon)\
**Post date:** [May 18, 2020, 9:05am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/1 "2020-05-18T09:05:14Z")

</div>

Hello, i have tried to import a CSV file into the table but i have experienced ValueError.

This is the script on the button:  
filepath= system.file.openFile(“csv”)  
string= system.file.readFileAsString(filepath)  
data = system.dataset.fromCSV(string)  
even.source.parent.getComponent(‘Table’).data = data

This is the data i would like to import into the table  
 ![image](https://us1.discourse-cdn.com/inductiveautomation/original/2X/7/7e77daf77d385fc48dc3f9f8085b6c083febf0ae.png)

---

<div class="post-metadata">

**Author:** ![MicroVAX\_II](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/microvax_ii/32/7319_2.png) [@MicroVAX\_II](https://forum.inductiveautomation.com/u/MicroVAX_II)\
**Post date:** [May 18, 2020, 9:52am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/2 "2020-05-18T09:52:43Z")

</div>

on the last line, the T in event is missing and ` (grave accent) instead of ’ (single quote)

filepath = system.file.openFile(“csv”)  
string = system.file.readFileAsString(filepath)  
data = system.dataset.fromCSV(string)  
event.source.parent.getComponent(‘Table’).data = data

The system.dataset.fromCSV has to be in a special format and not from an excel save as CSV.  
if you want to use the excel CSV format the link below page has an alternative script.  
[https://docs.inductiveautomation.com/display/DOC79/Exporting+and+Importing+a+CSV](https://docs.inductiveautomation.com/display/DOC79/Exporting+and+Importing+a+CSV)

---

<div class="post-metadata">

**Author:** ![MicroVAX\_II](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/microvax_ii/32/7319_2.png) [@MicroVAX\_II](https://forum.inductiveautomation.com/u/MicroVAX_II)\
**Post date:** [May 18, 2020, 10:09am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/3 "2020-05-18T10:09:54Z")

</div>

Weird, I copied the code I posed from ignition to the website, then i copied it from the website back into ignition and it screws up the single quote and double quotes.

---

<div class="post-metadata">

**Author:** ![MicroVAX\_II](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/microvax_ii/32/7319_2.png) [@MicroVAX\_II](https://forum.inductiveautomation.com/u/MicroVAX_II)\
**Post date:** [May 18, 2020, 10:35am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/4 "2020-05-18T10:35:40Z")

</div>

This worked for me.

> `# Import Python's built-in csv library.`
> 
> `import``csv`
> 
> `# Ask the user to find the CSV in the local file system.`
> 
> `path ` `=``system.` `file` `.openFile(` `"csv"` `)`
> 
> `# Create a reader object that will iterate over the lines of a CSV.`
> 
> `# We're using Python's built-in open() function to open the file.`
> 
> `csvData ` `=``csv.reader(` `open` `(path))`
> 
> `# Create a List of strings to use as a header for the dataset. Note that the number`
> 
> `# of headers must match the number of columns in the CSV, otherwise an error will occur.`
> 
> `# The simplest approach would be to use next() to read the first line in the file, and`
> 
> `# store that at the header.`
> 
> `header ` `=``csvData.` `next` `()`
> 
> `# Create a dataset with the header and the rest of our CSV.`
> 
> `dataset ` `=``system.dataset.toDataSet(header ,` `list` `(csvData))`
> 
> `# Store it into the table.`
> 
> `event.source.parent.getComponent(` `'Power Table'` `).data ` `=``dataset`

---

<div class="post-metadata">

**Author:** ![pturmel](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/pturmel/32/3295_2.png) [@pturmel](https://forum.inductiveautomation.com/u/pturmel)\
**Post date:** [May 18, 2020, 10:40am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/5 "2020-05-18T10:40:52Z")

</div>

> [@MicroVAX\_II](#):
>
> and it screws up the single quote and double quotes.

Yeah, you need to put code in a code formatting block. Basically, three backquotes by themselves on a line above and again on a line below your code. Like so:  
```  
pasted code  
```

See [this topic on discourse.org](https://meta.discourse.org/t/how-do-i-select-a-language-in-code-blocks/19247) for more formatting options.

---

<div class="post-metadata">

**Author:** ![Callmesoon](https://avatars.discourse-cdn.com/v4/letter/c/c67d28/32.png) [@Callmesoon](https://forum.inductiveautomation.com/u/Callmesoon)\
**Post date:** [May 19, 2020, 2:16am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/6 "2020-05-19T02:16:49Z")

</div>

Thank you so much for helping. Appreciate it

---

<div class="post-metadata">

**Author:** ![thiamweekhoo](https://avatars.discourse-cdn.com/v4/letter/t/e47c2d/32.png) [@thiamweekhoo](https://forum.inductiveautomation.com/u/thiamweekhoo)\
**Post date:** [June 24, 2021, 9:18am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/7 "2021-06-24T09:18:58Z")

</div>

Hello, I am using the script above.  
My question is can I import the .csv file to the Table Row 1 instead of the header.  
I am using the power table. The header data get replaced every time.  
FYI, my csv file does not contain header name.  
I do not want the data to overwrite my table header.

---

<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:** [June 24, 2021, 9:44am UTC](https://forum.inductiveautomation.com/t/how-do-i-import-csv-file-into-table/34830/8 "2021-06-24T09:44:21Z")

</div>

Crossposting is not allowed. Answer in the other thread.

> [@Reading from .csv file to table](http://forum.inductiveautomation.com/t/reading-from-csv-file-to-table/19614/16):
>
> You will need to define the headers manually, instead of using the file. Example: header = ['Col1', 'Col2', 'Col3']
