简体   繁体   中英

convert txt to dataframe in c#

I have a txt file and I want to save it to a dataframe that i make. Here is what i got so far:

public class DataOku
        {
            public StreamReader puanoku = new StreamReader("C:\\Users\\bsrkl\\OneDrive\\Masaüstü\\data\\data1.txt");
            public StreamReader iliskioku = new StreamReader("C:\\Users\\bsrkl\\OneDrive\\Masaüstü\\data\\data2.txt");


        DataOku()
        {
            string satir1;
            while ((satir1 = puanoku.ReadLine()) != null)
            {
                //because of the single line in the txt, i created an array for it
                string[] deger = satir1.Split(' ');

            }
            string satir2;
            while ((satir2 = iliskioku.ReadLine()) != null)
            {
                // because of the 2 dimensional array, i created a dataframe
                int[,] baglanti = new int[5, 5];
                baglanti????
            }
        }

data1.txt:

4 6 7 5 3 2

and data2.txt looks like this:

0 1 1 1 0 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 1 0
0 0 0 0 0 1
0 0 0 0 0 0

You are creating in your code a 2 dimensional array per each line, that's not correct as you need to create it only once. Also I presume you don't know the size of your data, so you need or to first compute the data size or use a more dynamic way of storing the data. I would use a list of arrays, like this:

List<int[]> baglanti = new List<int[]>();

while ((satir2 = iliskioku.ReadLine()) != null)
{
    string[] values = satir12.Split(' ');
    int[] iValues = new int[values.Length];

    for(int buc = 0; buc < values.Length; buc++)
        iValues[buc] = int.Parse(values[buc]);

    baglanti.Add(iValues);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM