# Reading tags

**URL:** <https://forum.inductiveautomation.com/t/reading-tags/4219>\
**Category:** Ignition\
**Created:** [March 29, 2012, 9:50am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219 "2012-03-29T09:50:36Z")\
**Posts on this page:** 18\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aliya](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@Aliya](https://forum.inductiveautomation.com/u/Aliya)\
**Post date:** [March 29, 2012, 9:50am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/1 "2012-03-29T09:50:36Z")

</div>

How to Read values from Text file and assign them back to plc tags??? ![: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:** [March 29, 2012, 9:56am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/2 "2012-03-29T09:56:33Z")

</div>

Can you give an example of a couple lines of the the csv file long with the tagnames you want to write to?

---

<div class="post-metadata">

**Author:** ![Aliya](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@Aliya](https://forum.inductiveautomation.com/u/Aliya)\
**Post date:** [March 29, 2012, 10:01am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/3 "2012-03-29T10:01:59Z")

</div>

here is the text file i hav attached. from this file i wanna read each value separately and want to assign them to plc tags  
[idle.txt](https://us1.discourse-cdn.com/inductiveautomation/original/2X/4/4fb4a76faae37ec4617df2cd8cab5608f5f1a9cd.txt) (92 Bytes)

---

<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:** [March 29, 2012, 10:10am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/4 "2012-03-29T10:10:17Z")

</div>

Ok, give me about a half-hour… 🙂

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 29, 2012, 10:11am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/5 "2012-03-29T10:11:06Z")

</div>

Does this file get appended to, so there is a new line every entry?

---

<div class="post-metadata">

**Author:** ![Aliya](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@Aliya](https://forum.inductiveautomation.com/u/Aliya)\
**Post date:** [March 29, 2012, 10:19am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/6 "2012-03-29T10:19:00Z")

</div>

no the file wont get appended… only the values ll b updated in one line of text file and i want to read these values in ignition and want to assign them to plc tags

---

<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:** [March 29, 2012, 10:26am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/7 "2012-03-29T10:26:11Z")

</div>

Okay, then. This will read a one-liner csv file and return the value as a list.

[code]  
def readCsvAsList(path):  
from [java.io](http://java.io) import BufferedReader,FileReader,File  
br = BufferedReader(FileReader(path))

data = line.split(’,’)

return data[/code]

Then you con use the system.tag.witeToTags function to do the rest:

`taglist=readCsvAsList("C:\idle.txt")
system.tag.writeToTags([Tag1,Tag2,Tag3,...],taglist)`

Edit: Whoops! Wrong script!

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 29, 2012, 11:04am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/8 "2012-03-29T11:04:02Z")

</div>

[quote=“JordanCClark”]Okay, then. This will read a one-liner csv file and return the value as a list.

[code]  
def readCsvAsList(path):  
from [java.io](http://java.io) import BufferedReader,FileReader,File  
br = BufferedReader(FileReader(path))

data = line.split(’,’)

return data[/code]

Then you con use the system.tag.witeToTags function to do the rest:

`taglist=readCsvAsList("C:\idle.txt")
system.tag.writeToTags([Tag1,Tag2,Tag3,...],taglist)`

Edit: Whoops! Wrong script![/quote]

Ill bet this doesn’t work. Where is line set. Might want to close br or you will leak memory.

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 29, 2012, 11:50am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/9 "2012-03-29T11:50:42Z")

</div>

[viewtopic.php?f=88&t=7885](http://www.inductiveautomation.com/forum/viewtopic.php?f=88&t=7885)

---

<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:** [March 29, 2012, 12:10pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/10 "2012-03-29T12:10:45Z")

</div>

[quote=“Kyle Chase”]  
Ill bet this doesn’t work. Where is line set. Might want to close br or you will leak memory.[/quote]

Yep, you’re right. Should be more like this:

[code]def readCsvAsList(path):  
from [java.io](http://java.io) import BufferedReader,FileReader,File  
br = BufferedReader(FileReader(path))

line = br.readLine()

data = line.split(’,’)

br.close()

return data[/code]

Thanks for the catch!

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 29, 2012, 12:12pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/11 "2012-03-29T12:12:11Z")

</div>

This wont work with his current file. readLine requires either a carriage return or a line feed, which his file does not have.

---

<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:** [March 29, 2012, 12:18pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/12 "2012-03-29T12:18:39Z")

</div>

That would be true if we were reading multiple lines. Since we only read one line, it really doesn’t matter if it terminates in a CR, LF, or EOF

 ![](https://us1.discourse-cdn.com/inductiveautomation/original/2X/a/a3217ada39fa0e85f79ae95c42d0ac592d48927e.png)

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 29, 2012, 12:27pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/13 "2012-03-29T12:27:50Z")

</div>

I stand corrected…

---

<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:** [March 29, 2012, 12:29pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/14 "2012-03-29T12:29:57Z")

</div>

No worries here. Actually, I love these discussions. If it helps us to to stretch ourselves, then it’s all worth while! 😃

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 29, 2012, 12:34pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/15 "2012-03-29T12:34:29Z")

</div>

The only other thing I would worry about is properly closing the file. Right now, you have no safety if an error occurs after opening the file.

In Java, I would do the following

```auto
try{
   BufferedReader reader = new BufferedReader(new FileReader(path));
   //parse data
}catch (IOException e){
   LOGGER.info("Something went wrong",e);
} finally {
   reader.close();
   reader = null;
}
```

---

<div class="post-metadata">

**Author:** ![Kyle\_Chase](https://avatars.discourse-cdn.com/v4/letter/k/9de0a6/32.png) [@Kyle\_Chase](https://forum.inductiveautomation.com/u/Kyle_Chase)\
**Post date:** [March 29, 2012, 12:37pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/16 "2012-03-29T12:37:08Z")

</div>

This should work, but doesnt. Maybe Jython 2.1 has no finally clause?

[code]  
def readCsvAsList(path):  
from [java.io](http://java.io) import BufferedReader,FileReader,File,IOException  
try:  
br = BufferedReader(FileReader(path))

```
  line = br.readLine()

  data = line.split(',')

```

except IOException:  
log something  
finally:  
br.close()

return data[/code]

---

<div class="post-metadata">

**Author:** ![Aliya](https://avatars.discourse-cdn.com/v4/letter/a/43a26b/32.png) [@Aliya](https://forum.inductiveautomation.com/u/Aliya)\
**Post date:** [March 30, 2012, 10:57am UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/17 "2012-03-30T10:57:38Z")

</div>

instead of using system.tag.writeToTag function for each tag path separately i want to use system.tag.writeToTags function coz i wanna read 300 tags from a text file.

---

<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:** [March 30, 2012, 2:47pm UTC](https://forum.inductiveautomation.com/t/reading-tags/4219/18 "2012-03-30T14:47:50Z")

</div>

> [@Aliya](#):
>
> instead of using system.tag.writeToTag function for each tag path separately i want to use system.tag.writeToTags function coz i wanna read 300 tags from a text file.

![:scratch:](https://us1.discourse-cdn.com/inductiveautomation/original/1X/76d1620bec252bfac07050c889ff5a91dc8a9c25.gif ":scratch:")

> [@JordanCClark](#):
>
> Then you can use the system.tag.witeToTags function to do the rest:
> 
> `taglist=readCsvAsList("C:\idle.txt")
> system.tag.writeToTags([Tag1,Tag2,Tag3,...],taglist)`
