# Parsing .csv and .txt files in FPMI

**URL:** <https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995>\
**Category:** Archived Topics\
**Created:** [July 1, 2010, 2:56am UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995 "2010-07-01T02:56:27Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Cas](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Cas](https://forum.inductiveautomation.com/u/Cas)\
**Post date:** [July 1, 2010, 2:56am UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/1 "2010-07-01T02:56:27Z")

</div>

Hi, everyone. I would like to use FPMI to open a .csv (comma delimited) or a .txt (tab delimited) file and parse each element into a text box on a FPMI window.

Imagine the simplest case: A file called Numbers.csv has thefollowing text: 1,2,3  
I want the user to open the file with the “fpmi.file.openFile()” command followed by the “fpmi.file.readFileAsString(path)” command. But then I want the 1 to go into one textbox on the window, the 2 to go into another, and the 3 to go into a third textbox.

It might get a little more complicated when we have multi-line CSV files with quotation marks, embedded commas, etc. (see attached files as examples)

I also want to be able to do the same with a tab delimited file.

How should this best be done? Assume I have more than enough discrete text boxes to hold all of the entries in the file. Perhaps there’s a magic parsing command in Python?

Thanks in advance,  
Cas  
[SimpleText (Tab delim).txt](https://us1.discourse-cdn.com/inductiveautomation/original/1X/f84ad084d2675323a745d81ac036ec4e2abbf233.txt) (94 Bytes)  
[SimpleText (comma delim).csv](https://us1.discourse-cdn.com/inductiveautomation/original/1X/a81f97723dbb5ad08c4a4685e11f666a3e5459f0.csv) (94 Bytes)

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [July 1, 2010, 3:27pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/2 "2010-07-01T15:27:53Z")

</div>

You can use straight Python file handling, here is an example for a csv file:[code]file = open(‘c:\file.csv’, ‘r’)

for line in file.readlines():  
data = line.split(",")  
num1 = data[0]  
num2 = data[1]  
num3 = data[2]  
print num1, num2, num3[/code]That will read a multi-line csv file. If you want to read just one line remove the loop and just do line = file.readlines(). The line.split will split the line on some character. If you wanted a tab instead do “\t”.

---

<div class="post-metadata">

**Author:** ![Cas](https://avatars.discourse-cdn.com/v4/letter/c/77aa72/32.png) [@Cas](https://forum.inductiveautomation.com/u/Cas)\
**Post date:** [July 1, 2010, 7:14pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/3 "2010-07-01T19:14:21Z")

</div>

Travis,  
Thank you, this is a great start. However, how can I have the code ignore commas that are in text surrounded by quotes (like in an address line, for instance)?

Right now, the line:  
“One, two”,“Two, three”,“Three, four”  
gets split into:  
num1 = “One  
num2 = two”  
num3 = "Two

where I’d like it to be:  
num1 = One, two  
num2 = Two, three  
num3 = Three, four

Any suggestions?  
Cas

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [July 1, 2010, 7:39pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/4 "2010-07-01T19:39:28Z")

</div>

There is the python csv module [http://docs.python.org/library/csv.html](http://docs.python.org/library/csv.html) that was created just for this.

Not sure if/how to include it into Ignition but it should give you some ideas.

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [July 1, 2010, 8:47pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/5 "2010-07-01T20:47:54Z")

</div>

The csv module in python would be great, however we don’t include that module in Ignition. The csv module depends on other libraries that we don’t have as well. So, if you can find a lighter weight one you can use that.

---

<div class="post-metadata">

**Author:** ![Robert](https://avatars.discourse-cdn.com/v4/letter/r/e95f7d/32.png) [@Robert](https://forum.inductiveautomation.com/u/Robert)\
**Post date:** [July 1, 2010, 8:53pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/6 "2010-07-01T20:53:22Z")

</div>

As a follow on, how can we include external python modules?  
Or can we?

---

<div class="post-metadata">

**Author:** ![Travis.Cox](https://avatars.discourse-cdn.com/v4/letter/t/b9bd4f/32.png) [@Travis.Cox](https://forum.inductiveautomation.com/u/Travis.Cox)\
**Post date:** [July 1, 2010, 8:59pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/7 "2010-07-01T20:59:53Z")

</div>

You can’t right now. I can add this as a feature request for Ignition.

---

<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:** [July 2, 2010, 7:48pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/8 "2010-07-02T19:48:11Z")

</div>

Please do! In IA’s tpyical timeliness, you’ve already answered my question! ![:prayer:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/e5a7e3b63facc151ea73e3b45db7bdb3d4bdc316.gif ":prayer:")😆

---

<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:** [July 2, 2010, 8:28pm UTC](https://forum.inductiveautomation.com/t/parsing-csv-and-txt-files-in-fpmi/1995/9 "2010-07-02T20:28:05Z")

</div>

Until it comes along did anyone look up importing a .csv file into MySQL/SQL Server?

[http://www.tech-recipes.com/rx/2345/import\_csv\_file\_directly\_into\_mysql/](http://www.tech-recipes.com/rx/2345/import_csv_file_directly_into_mysql/)

[http://www.sqlservercentral.com/Forums/Topic494460-338-1.aspx](http://www.sqlservercentral.com/Forums/Topic494460-338-1.aspx)
