Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have a text file that I grab from a machine it produces this:


Index CentMass    LowBound  Up Bound Crg(z) Ht   RelInt    Area    S/N Rat  Resolution Isotope Cluster Area
1    801.176819    800.98    801.29    1    77    0.39    483.69    31.38    10947.86    696.30
2    813.337097    813.25    813.62    1    32    0.16    248.36    11.53    7452.86     383.16
3    819.182800    818.94    819.32    1    84    0.42    680.44    38.91    8003.27    1109.00
4    821.186218    821.00    821.32    1    60    0.30    411.31    20.53    7745.72     406.33
5    824.232300    824.14    824.39    1    64    0.32    305.51    17.75    11502.74    216.63
6    837.184509    836.90    837.39    1    73    0.37    482.23    32.17    10486.57    675.95
7    846.059998    845.91    846.18    1    38    0.19    181.16    12.75    11362.17    198.22
8    850.221985    850.09    850.34    1    75    0.38    374.78    32.72    10521.18    632.60
9    850.470520    850.34    850.65    1    107    0.54    557.30    51.43    11015.11    982.53
10   852.218811    852.07    852.34    1    37    0.19    181.27    11.07    12988.61    163.57


...goes on....

125 2154.006348 2153.87 2154.38 1 11 0.06 83.10 10.45 10355.41 214.11

Background

From a mass spectrometer.
Using the code

I need to make it so each of those columns are put into an array, Im not really sure what is separating them or I would have just done a split at "," or something of that nature. When pasted into excell it becomes much clearer and neater, all the columns and rows are made neatly, unlike this view but you get the idea.

For code so far I just have it so at the push of Button1 it opens the file and then sends it to the clipboard ( the result above) and then a code that checks if its in text or array format. Some files come in array format and that is fully coded but I need it for when its in text format above.

Thanks for your consideration. I have some experience with code, but I am just unfamiliar with dealing with text files and the logic behind them. This will eventually be used to interact with python.


[edit]Code block added. I also shorted the table headers to fit the table data - OriginalGriff[/edit]
Posted
Updated 19-Jun-12 9:26am
v2
Comments
MaxAI 19-Jun-12 15:42pm    
Friend: Do you need each Row or Column put in an array. because if its each row you can lump the txt file into an Array, and use a foreach statement to seperator the Key/Peer matches by cycling through with the EOL statement "\r\n". I'm not sure if VB.Net has as verbose of text parsing has C# though

Me: each column needs to become a set group basically so CentroidMass and all beneath it has to be an array then height and all beneath it has to another array type thing just so i can pass it to python, which only takes a simple array from visual basic

There is no much special logic around text files. The format you show is purely ad-hoc, there are no real standards about it.

If you have a concern about particular way of separation of the words representing the numbers, you can easily read it line by line and then split every line by a set of separators at once, removing the empty elements, which will give you the desired result. Something like this:

C#
string line = //... read it
string[] words =
    string.Split(new char[] {' ', ',', ';', '\t', }, System.StringSplitOptions.RemoveEmptyEntries);
foreach(string word in words) {
    double value;
    if (double.TryParse(word, out value) {
        //... put the value where it should be, for example assign an array element to it
    } else {
        value = double.NaN; // I highly recommend using Not a Number to represent all kinds
                            // of invalid data
        //do something, maybe assign an array element to it, anyway
    } //if
} //loop

Please see:
http://msdn.microsoft.com/en-us/library/ms131448.aspx[^].

To read a text file, use System.IO.StreamReader,
http://msdn.microsoft.com/en-us/library/system.io.streamreader.aspx[^].

Good luck,
SA
 
Share this answer
 
v2
Comments
Maciej Los 19-Jun-12 16:24pm    
Good work! +5
Sergey Alexandrovich Kryukov 19-Jun-12 16:35pm    
Thank you, Maciej.
--SA
Pandvi 19-Jun-12 23:09pm    
Good work and nice link ! My 5!
Sergey Alexandrovich Kryukov 19-Jun-12 23:47pm    
Thank you, Pandvi.
--SA
VJ Reddy 20-Jun-12 13:22pm    
Good answer. 5!
I think you're looking for something like this:

VB
Public Sub ReadAllEntries()

        Dim appPath As String = "c:\yourFile.txt"
        Dim fileEntries As New List(Of String)

        If Not File.Exists(appPath) Then
            Exit Sub
        End If

        Try
            ' Read the file into a list...
            Dim reader As StreamReader = New StreamReader(appPath)
            fileEntries.Clear()

            Do Until reader.Peek = -1 'Until eof
                fileEntries.Add(reader.ReadLine)
            Loop

            reader.Close()

        Catch ex As Exception
            ' The file's empty.
        End Try

        ' Now we have the whole file in a list(Of String)
        Dim centMass As New List(Of String)
        Dim LowBound As New List(Of String)
        Dim UpBound As New List(Of String)
        Dim Crg As New List(Of String)
        Dim Ht As New List(Of String)
        Dim RelInt As New List(Of String)
        Dim Area As New List(Of String)
        Dim SNRat As New List(Of String)
        Dim Resolution As New List(Of String)
        Dim Isotope As New List(Of String)

        For Each line As String In fileEntries
            If line.Substring(0, 5) <> "Index" then
                If line.Substring(0, 7) <> "Cluster" then
                    ' Ok - now it seems that each group of numbers is separated by 3 or 4 white spaces.
                    ' Let's convert all those to just one:
                    line = line.Replace("    ", " ") ' Those groups of 4 white spaces becomes one...
                    line = line.Replace("   ", " ") ' Those groups of 3 become just one...

                    Dim parts() As String = Split(line, " ")

                    ' Add the data to your lists:
                    Try
                        centMass.Add(parts(1))
                        LowBound.Add(parts(2))
                        UpBound.Add(parts(3))
                        Crg.Add(parts(4))
                        Ht.Add(parts(5))
                        RelInt.Add(parts(6))
                        Area.Add(parts(7))
                        SNRat.Add(parts(8))
                        Resolution.Add(parts(9))
                        Isotope.Add(parts(10))
                    Catch ex As Exception
                        ' If this fials, we're not where we want to be in the file anyway.
                    End Try
                    
                End If
            End If
        Next

    End Sub
 
Share this answer
 
v3
Comments
MaxAI 25-Jun-12 10:38am    
Worked amazingly, had to make a few adjustments since it had to pass through the data explorer into the clipboard but thank you soooooooo much this is perfect. Also this really opened my eyes to this logic, I don't know how I didn't get it when its this simple in front of me!!!
pdoxtader 25-Jun-12 10:46am    
Glad I could help. Sometimes a fresh perspective from someone who's not so invested in the project helps out a lot. If you consider my answer your solution, please click the green button...

- Pete
File would appear to be tab delimited. You can attempt to split at "\t" the same way you would a ",".
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900