Articles

Solved how would I extract just xyz values from a section of a file

November 27, 2012 at 06:49:25
Specs: Windows 7, 4.17Ghz 8GB ram

I have an input file that looks like this:
ID= 57010101-1
X= 0.0005
Y= 0.0006
Z= 0.0000
Avg= -0.0058

ID= 57010101-2
X= 0.0005
Y= 0.0005
Z= -0.0001
Avg= -0.0057

ID= 57010101-3
X= 0.0005
Y= 0.0007
Z= -0.0001
Avg= -0.0056
C= -0.0031

I need to extract the xyz from the id.

I have been messing with this for a while now but i cannot get it to grab it from the specific id that i give it.

Any suggestions?

I am using vb.net


See More: how would I extract just xyz values from a section of a file

Report •


✔ Best Answer
November 27, 2012 at 17:07:00

You know, I've never really learned any good methods to parse text files, but even I know it's best to parse the entire file once and be done with it.

Consider the following command line program:

Option Infer On
Module Module1
  Sub Main()
    Dim vals = parseFile("c:\some.txt")
    With vals("57010101-2")
      Console.WriteLine("X={0} Y={1} Z={2}", .X, .Y, .Z)
    End With
    Console.ReadKey(True)
  End Sub

  Structure valueSet
    Public X As Double?
    Public Y As Double?
    Public Z As Double?
    Public ReadOnly Property IsValid As Boolean
      Get
        Return X.HasValue AndAlso Y.HasValue AndAlso Z.HasValue
      End Get
    End Property
  End Structure

  Function parseFile(filePath As String) As Dictionary(Of String, valueSet)
    Dim ret = New Dictionary(Of String, valueSet)
    Dim file = New System.IO.StreamReader(filePath)
    Dim id As String
    Dim values = New valueSet

    Do While String.IsNullOrEmpty(id) AndAlso Not file.EndOfStream
      Dim line = Split(file.ReadLine)
      If line.Length = 2 AndAlso String.Equals("ID=", line(0), _
               StringComparison.CurrentCultureIgnoreCase) Then _
          id = line(1)
    Loop

    Do Until file.EndOfStream
      Dim line = Split(file.ReadLine)
      If line.Length = 2 Then
        Select Case line(0).ToUpperInvariant()
          Case "ID=".ToUpperInvariant()
            If values.IsValid Then _
                ret.Add(id, values)
            id = line(1) : values = New valueSet
          Case "X=".ToUpperInvariant()
            values.X = Double.Parse(line(1))
          Case "Y=".ToUpperInvariant()
            values.Y = Double.Parse(line(1))
          Case "Z=".ToUpperInvariant()
            values.Z = Double.Parse(line(1))
        End Select
      End If
    Loop

    If values.IsValid AndAlso Not ret.ContainsKey(id) Then _
        ret.Add(id, values)
    Return ret
  End Function
End Module

How To Ask Questions The Smart Way



#1
November 27, 2012 at 07:20:23

Just read the file line by line until you find the ID that you are looking for. Then read the next 3 lines and extract the X, Y, Z values.

Report •

#2
November 27, 2012 at 07:28:27

I'm not quite sure how to do that. That's the part I am stuck on

Report •

#3
November 27, 2012 at 08:41:41

Which part don't you know how to do? Read lines from a text file into a string? Search the string for certain characters or strings? Extract part of a string?

Google will help you with all of those.


Report •

Related Solutions

#4
November 27, 2012 at 08:54:12

how to read the next 3 lines after

Report •

#5
November 27, 2012 at 09:31:53

Function GetValue(ByVal val As String) As String
        ListBox1.Items.Clear()
        val &= " "
        For Each line In IO.File.ReadAllLines("C:\Users\User\Desktop\result.txt")
            If line.Contains(TextBox1.Text) Then
                Msgbox(line)
            End If
        Next
    End Function

With that I can find the line. But Im not sure how to get the xyz from the next few lines


Report •

#6
November 27, 2012 at 12:33:38

This is what I came up with:

Sub linestr()
        Dim c As Integer = 1
        Dim sr As New System.IO.StreamReader(input)
        Dim line As String = String.Empty
        Dim lines As String()

        ListBox1.Items.Clear()

        line = sr.ReadLine()
        Do While (Not line Is Nothing)
            If line.Contains(TextBox1.Text) Then
                Label1.Text = c.ToString()

                lines = IO.File.ReadAllLines(input)

                Label2.Text = lines(c)

                ListBox1.Items.Add(lines(c + 2)) 'X
                ListBox1.Items.Add(lines(c + 3)) 'Y
                ListBox1.Items.Add(lines(c + 4)) 'Z

            End If
            line = sr.ReadLine()
        Loop
    End Sub

Any suggestions on a better way?


Report •

#7
November 27, 2012 at 12:34:03

is that powershell? (I am totally clueless on powershell,as you can tell.)
Since I don't know that script, I'll try to wing it:
Function GetValue(ByVal val As String) As String
ListBox1.Items.Clear()
val &= " "
x=0
For Each line In IO.File.ReadAllLines("C:\Users\User\Desktop\result.txt")
If line.Contains(TextBox1.Text) Then
x=1
Msgbox(line)
End If
if x>0 then
if x>1 and x<5 then msgbox(line)
x=x+1
end if
Next
End Function

I know, that is atrocious coding, but like I said, I don't know anything about this language, or even if this will work or not. I guess I need to download or otherwise obtain powershell documentation.


Report •

#8
November 27, 2012 at 12:39:28

No, its VB.NET but I think your code is cleaner than mine. I feel like I over complicated it.

Report •

#9
November 27, 2012 at 14:33:33

Oh. I'm so far out of the loop, I think I just passed Pluto! ;-)
I didn't know if you could use subscripts on the lines. In that case, this might be an improvement (assuming it works):
lines=io.file.readalllines(input)
'this next might need to be: for i=0 to lines.count
for i=0 to ubound(lines)
'and this might need to be: if instr(lines(i),textbox1.text) then...
if lines(i).contains(textbox1.text) then
for L=i+1 to i+3
msgbox(lines(L))
next
exit for
end if
next

Report •

#10
November 27, 2012 at 17:07:00
✔ Best Answer

You know, I've never really learned any good methods to parse text files, but even I know it's best to parse the entire file once and be done with it.

Consider the following command line program:

Option Infer On
Module Module1
  Sub Main()
    Dim vals = parseFile("c:\some.txt")
    With vals("57010101-2")
      Console.WriteLine("X={0} Y={1} Z={2}", .X, .Y, .Z)
    End With
    Console.ReadKey(True)
  End Sub

  Structure valueSet
    Public X As Double?
    Public Y As Double?
    Public Z As Double?
    Public ReadOnly Property IsValid As Boolean
      Get
        Return X.HasValue AndAlso Y.HasValue AndAlso Z.HasValue
      End Get
    End Property
  End Structure

  Function parseFile(filePath As String) As Dictionary(Of String, valueSet)
    Dim ret = New Dictionary(Of String, valueSet)
    Dim file = New System.IO.StreamReader(filePath)
    Dim id As String
    Dim values = New valueSet

    Do While String.IsNullOrEmpty(id) AndAlso Not file.EndOfStream
      Dim line = Split(file.ReadLine)
      If line.Length = 2 AndAlso String.Equals("ID=", line(0), _
               StringComparison.CurrentCultureIgnoreCase) Then _
          id = line(1)
    Loop

    Do Until file.EndOfStream
      Dim line = Split(file.ReadLine)
      If line.Length = 2 Then
        Select Case line(0).ToUpperInvariant()
          Case "ID=".ToUpperInvariant()
            If values.IsValid Then _
                ret.Add(id, values)
            id = line(1) : values = New valueSet
          Case "X=".ToUpperInvariant()
            values.X = Double.Parse(line(1))
          Case "Y=".ToUpperInvariant()
            values.Y = Double.Parse(line(1))
          Case "Z=".ToUpperInvariant()
            values.Z = Double.Parse(line(1))
        End Select
      End If
    Loop

    If values.IsValid AndAlso Not ret.ContainsKey(id) Then _
        ret.Add(id, values)
    Return ret
  End Function
End Module

How To Ask Questions The Smart Way


Report •

#11
November 29, 2012 at 06:11:34

I like that. I will give it a try. Thank you

Report •

#12
November 29, 2012 at 11:54:11

Sure. Just save the resulting Dictionary in the module level, so you're not parsing the same file every time the user hits the button.

How To Ask Questions The Smart Way


Report •


Ask Question