如何垂直或逐列读取文本文件的数据

时间:2022-01-21 15:50:21

How can we read a text file column by column.

我们如何逐列读取文本文件。

Check my new code: I can read the data row-wise using text.split (' ') But how can be the file read as column wise? Lets assume that a file contains number of rows and columns but I was able to read the data/value horizontally. The code you see that below that's what I could execute! SEE THE CODE BELOW:-

检查我的新代码:我可以使用text.split('')逐行读取数据但是如何将文件作为列方式读取?让我们假设一个文件包含多少行和列,但我能够水平读取数据/值。您在下面看到的代码就是我可以执行的代码!请参阅以下代码: -

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {


        string text = "";
        text = textBox1.Text;

        string[] arr = text.Split(' ');
        textBox2.Text = arr[5];
        textBox3.Text = arr[8];
    }

    private void button3_Click(object sender, EventArgs e)
    {


        string file_name = "c:\\Excel\\count.txt";
        string txtline = "";

        System.IO.StreamReader objreader;
        objreader = new System.IO.StreamReader(file_name);

        do
        {
            txtline = txtline + objreader.ReadLine() + "\r\n";
            txtline = txtline + objreader.ReadToEnd() + "";
            this.textBox1.Text = "subzihut";

        }
        while (objreader.Peek() != -1);
        textBox1.Text = txtline;
        objreader.Close();

        }

    private void button2_Click(object sender, EventArgs e)
    {
     textBox4.Text = textBox2.Text + " " + textBox3.Text;
    }
    }
    }

5 个解决方案

#1


A textfile contains a sequence of characters, delimited by newline characters and probably other characters which are used as delimiters (usually a comma or a semiciolon).

文本文件包含一系列字符,由换行符分隔,可能还有其他字符用作分隔符(通常是逗号或半字母)。

When you read a file you simply read this stream of characters. There are helper functions which read such a file line-by-line (using the newline character as a delimiter).

当您阅读文件时,您只需阅读此字符流。有一些辅助函数可以逐行读取这样的文件(使用换行符作为分隔符)。

In plain .Net there are no methods which read column-by-column.

在普通的.Net中,没有任何方法可以逐列读取。

So you should:

所以你应该:

  1. read the file line by line
  2. 逐行读取文件

  3. split each line into fields/columns using string.Split() at the separator character(s)
  4. 使用string.Split()在分隔符处将每行拆分为字段/列

  5. access only the columns of interest
  6. 只访问感兴趣的列

#2


You can simply read the file line by line, splitt the lines and do whatever you want.

您可以逐行读取文件,拆分行并执行任何操作。

var lines = File.ReadLines(@"c:\yourfile.txt");
foreach(var line in lines)
{
    var values = line.Split(' ');
}

#3


Firstly, it's always best to post code you've attempted, the desired result, and the problem you're having. * is not a code writing service.

首先,最好发布您尝试过的代码,所需的结果以及您遇到的问题。 *不是代码编写服务。

However, it's true that sometimes you just don't know where to start. Here are some pointers.

但是,有时你只是不知道从哪里开始。这里有一些指示。

  1. You'll have to read the whole file in, probably using something like a StreamReader.
  2. 你必须阅读整个文件,可能使用像StreamReader这样的东西。

  3. You can parse the first row into column names. Use StreamReader.ReadLine() to get the first line and then do some simple string parsing on it.
  4. 您可以将第一行解析为列名。使用StreamReader.ReadLine()获取第一行,然后对其进行一些简单的字符串解析。

  5. You'll want to create some kind of class/object to store and access your data.
  6. 您需要创建某种类/对象来存储和访问您的数据。

  7. Once you have column names, continue to parse the following lines into the proper arrays.
  8. 获得列名后,继续将以下行解析为正确的数组。

Some here's a rough idea

这里有些是个粗略的想法

using(StreamReader sr = new StreamReadeR("C:\\my\\file\\location\\text.csv"))
{
    string header = sr.ReadLine();
    List<string> HeaderColumns = new List<string>(header.split(" ", StringSplitOptions.RemoveEmptyEntires));
    myModelClass.Header = HeaderColumns;

etc...

You might also consider making some kind of dictionary to access columns by header name and index.

您还可以考虑使用某种字典来按标题名称和索引访问列。

#4


public string getColumnString(int columnNumber){

    string[] lines = System.IO.ReadAllLines(@"C:\inputfile.txt");
    string stringTobeDisplayed = string.Empty;

    foreach(string line in lines) {
        if(columnNumber == -1){ //when column number is sent as -1 then read full line
           stringTobeDisplayed +=  line +"\n"
        }
        else{  //else read only the column required
            string [] words = line.Split();
            stringTobeDisplayed += word[columnNumber] +"\n"
       }
    }

  return stringTobeDisplayed;
}

#5


Maybe this will help you:

也许这会对你有所帮助:

public static void ReadFile(string path)
    {
        List<string> Col1 = new List<string>();
        List<string> Col2 = new List<string>();
        List<string> Col3 = new List<string>();
        using (StreamReader sr = new StreamReader(path))
        {
            while (sr.EndOfStream)
            {
                string header = sr.ReadLine();
                var values = header.Split(' ');
                Col1.Add(values[0]);
                Col2.Add(values[1]);
                Col3.Add(values[2]);
            } 
        }
    }

#1


A textfile contains a sequence of characters, delimited by newline characters and probably other characters which are used as delimiters (usually a comma or a semiciolon).

文本文件包含一系列字符,由换行符分隔,可能还有其他字符用作分隔符(通常是逗号或半字母)。

When you read a file you simply read this stream of characters. There are helper functions which read such a file line-by-line (using the newline character as a delimiter).

当您阅读文件时,您只需阅读此字符流。有一些辅助函数可以逐行读取这样的文件(使用换行符作为分隔符)。

In plain .Net there are no methods which read column-by-column.

在普通的.Net中,没有任何方法可以逐列读取。

So you should:

所以你应该:

  1. read the file line by line
  2. 逐行读取文件

  3. split each line into fields/columns using string.Split() at the separator character(s)
  4. 使用string.Split()在分隔符处将每行拆分为字段/列

  5. access only the columns of interest
  6. 只访问感兴趣的列

#2


You can simply read the file line by line, splitt the lines and do whatever you want.

您可以逐行读取文件,拆分行并执行任何操作。

var lines = File.ReadLines(@"c:\yourfile.txt");
foreach(var line in lines)
{
    var values = line.Split(' ');
}

#3


Firstly, it's always best to post code you've attempted, the desired result, and the problem you're having. * is not a code writing service.

首先,最好发布您尝试过的代码,所需的结果以及您遇到的问题。 *不是代码编写服务。

However, it's true that sometimes you just don't know where to start. Here are some pointers.

但是,有时你只是不知道从哪里开始。这里有一些指示。

  1. You'll have to read the whole file in, probably using something like a StreamReader.
  2. 你必须阅读整个文件,可能使用像StreamReader这样的东西。

  3. You can parse the first row into column names. Use StreamReader.ReadLine() to get the first line and then do some simple string parsing on it.
  4. 您可以将第一行解析为列名。使用StreamReader.ReadLine()获取第一行,然后对其进行一些简单的字符串解析。

  5. You'll want to create some kind of class/object to store and access your data.
  6. 您需要创建某种类/对象来存储和访问您的数据。

  7. Once you have column names, continue to parse the following lines into the proper arrays.
  8. 获得列名后,继续将以下行解析为正确的数组。

Some here's a rough idea

这里有些是个粗略的想法

using(StreamReader sr = new StreamReadeR("C:\\my\\file\\location\\text.csv"))
{
    string header = sr.ReadLine();
    List<string> HeaderColumns = new List<string>(header.split(" ", StringSplitOptions.RemoveEmptyEntires));
    myModelClass.Header = HeaderColumns;

etc...

You might also consider making some kind of dictionary to access columns by header name and index.

您还可以考虑使用某种字典来按标题名称和索引访问列。

#4


public string getColumnString(int columnNumber){

    string[] lines = System.IO.ReadAllLines(@"C:\inputfile.txt");
    string stringTobeDisplayed = string.Empty;

    foreach(string line in lines) {
        if(columnNumber == -1){ //when column number is sent as -1 then read full line
           stringTobeDisplayed +=  line +"\n"
        }
        else{  //else read only the column required
            string [] words = line.Split();
            stringTobeDisplayed += word[columnNumber] +"\n"
       }
    }

  return stringTobeDisplayed;
}

#5


Maybe this will help you:

也许这会对你有所帮助:

public static void ReadFile(string path)
    {
        List<string> Col1 = new List<string>();
        List<string> Col2 = new List<string>();
        List<string> Col3 = new List<string>();
        using (StreamReader sr = new StreamReader(path))
        {
            while (sr.EndOfStream)
            {
                string header = sr.ReadLine();
                var values = header.Split(' ');
                Col1.Add(values[0]);
                Col2.Add(values[1]);
                Col3.Add(values[2]);
            } 
        }
    }