Announcement

Collapse
No announcement yet.

Parsing a CSV file for specific row data

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    Parsing a CSV file for specific row data

    I have been tweaking an existing HS3 script from this forum to parse my utility company's regularly changing CSV file. It reports meter data from multiple sites in our portfolio. I am trying to parse just the data from one specific site to set device values and email. I was successful using the following Split String script below for a few days as the site data I wanted was the first line in the CSV but then I noticed the meter provider would change my site ID to another row in the CSV file but the column data was the same attributes. Essentially I was reporting on another site for a couple of days before I noticed.

    Now I am searching to find a way in the script to search and match the site ID number 657772 that is always in the first column and then parse specific variable data for that row only. Is this possible and if so I could sure use some help as I have exhausted trying different methods but cant quite come up with anything that works.

    Code:
    Imports System.IO
    
    Sub Main(ByVal Parm As Object)
    
    Dim SplitLine() As String = System.IO.File.ReadAllLines("D:\Meterdata\SiteID_657772\Export.csv")
    
    Dim SplitChars1() As String = SplitLine(1).Split(",") *my site data used to be in first line
    
    hs.writelog("MV", SplitChars1(12)) 'volts
    hs.writelog("MKW", SplitChars1(13)) 'KW
    hs.writelog("MKVA", SplitChars1(14)) 'KVA
    hs.writelog("MKWH", SplitChars1(15)) 'KWH
    
    hs.setdevicevaluebyref(3746, SplitChars1(12), True)
    hs.setdevicevaluebyref(3747, SplitChars1(13), True)
    hs.setdevicevaluebyref(3748, SplitChars1(14), True)
    hs.setdevicevaluebyref(3749, SplitChars1(15), True)
    
    End sub
    Thank you for any help.

    Will


    #2
    Take a look at TextFieldParser here: https://docs.microsoft.com/en-us/dot...ted-text-files

    A Google search will provide lots of examples.
    "if I have seen further [than others], it is by standing on the shoulders of giants." --Sir Isaac Newton (1675)

    Comment


      #3
      You should be able to use something like:

      Code:
          Imports System.IO
      
          Sub Main(ByVal Parm As Object)
              Dim Line As String = ""
              For Each Line In File.ReadLines("D:\Meterdata\SiteID_657772\Export.csv")
                  If Line.Contains("657772") Then Exit For
              Next
              Dim SplitChars1() As String = Line.Split(",")
              ' continue here
          End Sub
      Depending on how the CSV is formatted, you can change
      Code:
      If Line.Contains("657772") Then Exit For
      To

      Code:
      If Line.Startswith("657772") Then Exit For
      Jon

      Comment


        #4
        Very nice, thank you Jon. Looks like your revision works right out of the box...I should get a CSV update in the next few hours to confirm all is well. You are the man!

        Will

        Comment


          #5
          Originally posted by will40 View Post
          Very nice, thank you Jon. Looks like your revision works right out of the box...I should get a CSV update in the next few hours to confirm all is well. You are the man!

          Will
          Great!
          Jon

          Comment


            #6
            just got our meter updates and the script works great! I'll be visiting your site soon for your data graphing utility to polish this off. Thanks again Jon!

            Comment

            Working...
            X