C#读取txt文本文件中的数据

原始数据文件是txt文件,存储的方式如下:-----2012年01月01日温度_T=19资料----------00-01-02572066.4(6.6/6.4)6.3(... 原始数据文件是txt文件,存储的方式如下:
-----2012年01月01日温度_T=19 资料----
----- -00 -01 -02
57206 6.4(6.6/6.4) 6.3(6.5/6.3) 6.3(6.3/6.3)
57204 4.4(4.6/4.4) 4.3(4.4/4.3) 4.3(4.3/4.3)
57217 6.2(6.5/6.2) 5.8(6.2/5.8) 5.1(5.8/5.0)
57208 5.5(5.6/5.5) 5.4(5.5/5.4) 5.3(5.4/5.3)
57303 -6.2(-6.5/-6.2) -6.1(-6.2/-6.0) -5.9(-6.1/-5.9)
56196 6.0(6.0/5.9) 5.9(6.0/5.9) 5.9(5.9/5.9)
56193 5.8(6.0/5.7) 5.6(5.8/5.6) 5.4(5.6/5.4)
56194 5.6(5.7/5.5) 5.5(5.6/5.5) 5.2(5.5/5.2)

现在我需要用C#语言将第3行到第10的数据读取出来存入数组里面,然后再求平均值。我目前遇到的难点有3点:
第一行和第二行可以不用读取,那如何跳过前面两行,直接从第三行开始读取。
第三行到第十行里面的每个数据如何读取,我搜了好多资料,讲的都是一整行一整行的读取,而我需要得到其中的每个数据。
里面带负号的数据又如何来读取。
我刚刚接触C#语言,好多东西都不是很懂,而且老板又催的很紧,所以就只有以发帖的方式请教大神了。
展开
 我来答
百度网友faadf46
高粉答主

2019-09-12 · 说的都是干货,快来关注
知道答主
回答量:4556
采纳率:0%
帮助的人:65.4万
展开全部

1、首先先来看一下准备的txt文本文件的内容。

2、然后在程序中引入操作文件的命名空间System.IO。

3、接下来需要定义一个变量,存储文件所在的路径。

4、然后先读取文本内容,调用File类的ReadAllLines即可读取所有内容。

5、接下来是写入内容,按照下图的方式,准备要写入的内容。

6、准备好内容以后,调用File的WriteAllLines进行内容的写入。

玩转数据处理
推荐于2017-12-16 · 数据处理,Python,dotnet
玩转数据处理
采纳数:1613 获赞数:3792

向TA提问 私信TA
展开全部
List<string> listLines = new List<string>();
using(StreamReader reader = new StreamReader ("your text file's path"))
{
    int i = 1;
    string line = reader.ReadLine();
    while(line!="" && line != nul)
    {
        if(i>=3)
        {
            listLines.Add(line);
        }
        line = reader.ReaderLine();
    }
    //循环完后,listLines 里面就放有第三行到第十行的数据了
}
for(int i = 0; i< listLines.Count ; i++)
{
    //listLines[i] 你想怎么处理就怎么处理咯
}
更多追问追答
追问
谢谢您,刚刚按着您写的,现在是解决了第一个问题,就是把第三行到第十行的数据存到listLines这个里面了,那接下来,每一行里面有括号,有空格,有负号,这些又如何来处理呢?
追答
List<string> listLines = new List<string>();
using(StreamReader reader = new StreamReader ("your text file's path"))
{
    int i = 1;
    string line = reader.ReadLine();
    while(line!="" && line != nul)
    {
        if(i>=3)
        {
            listLines.Add(line);
        }
        line = reader.ReaderLine();
    }
    //循环完后,listLines 里面就放有第三行到第十行的数据了
}
for(int i = 0; i< listLines.Count ; i++)
{
    string temp = listLines[i];
    //string temp = "57206 6.4(6.6/6.4)       6.3(6.5/6.3)       6.3(6.3/6.3)";
    int numberLength = temp.IndexOf(' ')+1;
    int tempLength = temp.Length;
    string number1 = temp.Substring(0, numberLength);
    
    temp = temp.Substring(numberLength, tempLength - numberLength);
    numberLength = temp.IndexOf('(');
    tempLength = temp.Length;
    string number2 = temp.Substring(0, numberLength);
    temp = temp.Substring(numberLength, tempLength - numberLength);
    //...................后面的其他数自个慢慢调吧
}
本回答被网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
freeeeeewind
2015-08-22 · TA获得超过1万个赞
知道大有可为答主
回答量:3227
采纳率:94%
帮助的人:1286万
展开全部

下面的控制台应用程序实现要求的功能

using System;
using System.IO;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            // 新建一个DataTable
            DataTable tb = new DataTable();
            // 添加一列用于存放读入的浮点数
            DataColumn c= tb.Columns.Add("Value", typeof(double));

            // 打开文件准备读取数据   
            StreamReader rd = File.OpenText(@"d:\data.txt");
            string line;
            // 循环读出文件的每一行
            while ((line = rd.ReadLine()) != null)
            {
                // 拆分出一行的所有用空格分割的数据项
                string[] values = line.Split(' ');
                // 将每个数据项转换成浮点数,并存入DataTable
                foreach (string s in values)
                {
                    if (!string.IsNullOrEmpty(s))
                    {
                        // 转换成浮点数
                        double v = double.Parse(s);
                        // 存入DataTable
                        DataRow r = tb.NewRow();
                        r["Value"] = v;
                        tb.Rows.Add(r);
                    }
                }
            }
            rd.Close();
             //输出DataTable中保存的数组
            foreach (DataRow r in tb.Rows)
            {
                Console.WriteLine(r["Value"]);
            }
        }
    }
}

D:\Data.Txt内容为

程序运行结果

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式