阅读文本文件,然后在VB .NET中拆分 [英] Reading Text File then Spliting it up in VB .NET

查看:110
本文介绍了阅读文本文件,然后在VB .NET中拆分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我有一个文本文件,该文件是从生成此文件的机器上获取的:


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



...继续....

125 2154.006348 2153.87 2154.38 1 11 0.06 83.10 10.45 10355.41 214.11

背景

来自质谱仪.
使用代码

我需要这样做,以便将这些列中的每一个都放入一个数组中,我不太确定是什么在分隔它们,否则我将只是在,"或类似性质的地方进行了拆分.粘贴到excell中后,它变得更加清晰和整洁,所有列和行都整齐地制作,与这种视图不同,但是您可以理解.

对于到目前为止的代码,我只需要按一下Button1就可以打开文件,然后将其发送到剪贴板(上面的结果),然后再检查它是否为文本或数组格式的代码.有些文件采用数组格式,并且已经过完全编码,但是当我使用上面的文本格式时,我需要它.

感谢您的考虑.我有一些代码方面的经验,但是我不熟悉处理文本文件及其背后的逻辑.最终将用于与python交互.


[edit]已添加代码块.我还缩短了表标题以适合表数据-OriginalGriff [/edit]



...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]

推荐答案

文本文件周围没有太多特殊的逻辑.您显示的格式纯粹是临时的,没有真正的标准.

如果您对表示数字的单词的特定分离方式有所顾虑,可以轻松地逐行阅读,然后通过一组分隔符一次将每一行拆分,除去空元素,这将为您提供理想的结果.像这样的东西:

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:

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


请参阅:
http://msdn.microsoft.com/en-us/library/ms131448.aspx [ ^ ].

要读取文本文件,请使用System.IO.StreamReader
http://msdn.microsoft.com/en-us/library/system.io. streamreader.aspx [ ^ ].

祝你好运,

SA


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


我认为您正在寻找这样的东西:

I think you''re looking for something like this:

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


文件似乎是制表符分隔.您可以尝试以与,"相同的方式在"\ t"处拆分.
File would appear to be tab delimited. You can attempt to split at "\t" the same way you would a ",".


这篇关于阅读文本文件,然后在VB .NET中拆分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆