【问题标题】:Reading from and manipulating a .csv file读取和操作 .csv 文件
【发布时间】:2013-05-08 00:25:40
【问题描述】:

我每个月都有多个 .csv 文件,如下所示:

01/04/2012,00:00,7.521527,80.90972,4.541667,5.774305,7,281.368
02/04/2012,00:00,8.809029,84.59028,6.451389,5.797918,7,274.0764
03/04/2012,00:00,4.882638,77.86806,1.152778,15.13611,33,127.6389
04/04/2012,00:00,5.600694,50.35417,-3.826389,15.27222,33,40.05556

格式为:日期格式为dd/mm/yy、当前时间、当前温度、当前湿度、当前露点、当前风速、当前阵风、当前风向

程序需要计算平均值 温度 湿度 风速 风向

并将它们显示在文本框中。

有什么想法吗?

这是我到目前为止所做的......

Option Strict On
Option Explicit On

Imports System.IO
Imports System

Public Class Form1   

Private Sub cmb1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmb1.SelectedIndexChanged

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnexit.Click
    Me.Close()
End Sub

Private Sub btn1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btndata.Click
    'This is for August
    If cmb1.SelectedIndex = 1 Then
        TextBox1.Clear()
        Using reader As New StreamReader("c://temp/DailyAug12log.csv")
            Dim line As String = reader.ReadLine()
            Dim avgTemp As Integer
            Dim fields() As String = line.Split(",".ToCharArray())
            Dim fileDate = CDate(fields(0))
            Dim fileTime = fields(1)
            Dim fileTemp = fields(2)
            Dim fileHum = fields(3)
            Dim fileWindSpeed = fields(4)
            Dim fileWindGust = fields(5)
            Dim fileWindBearing = fields(6)

            While line IsNot Nothing
                counter = counter + 1
                line = reader.ReadLine()
            End While
            avgTemp = CInt(fields(2))
            avgTemp = CInt(CDbl(avgTemp / counter))
            TextBox1.Text = TextBox1.Text & "Month = August" & vbCrLf & "Temperature Average: " & avgTemp & vbCrLf
        End Using
    End If
End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Dim files() As String
    files = Directory.GetFiles("C:\Temp", "*.csv", SearchOption.AllDirectories)
    For Each FileName As String In files
        cmb1.Items.Add(FileName.Substring(FileName.LastIndexOf("\") + 1, FileName.Length - FileName.LastIndexOf("\") - 1))
    Next
End Sub
End Class

【问题讨论】:

  • 您需要另一个变量来跟踪您已阅读的行数。只需为每一行增加一个。然后将您的“总”变量除以行数,并使用计算值更新您的文本框。您也可以声明其他“总计”变量来总计其他测量值...
  • 感谢我正在努力!关于如何添加它的任何想法?谢谢!
  • 严重吗?...在​​与“total”变量相同的位置/时尚中声明行的计数器。它将是整数类型。在您累积总变量的同一位置将该变量加一。你认为你应该计算while循环内部还是外部的平均值?平均值应该使用什么数据类型?整数还是双精度数?
  • 您好,我试过了,但我的计数器不工作。它将我的平均值计算为 0。我在 while 循环内有计数器,在循环外计算平均值。你能给我一个你认为我应该做什么的例子吗?
  • 我已经用我现在拥有的代码更新了上面的代码,谢谢

标签: vb.net


【解决方案1】:
Private Class Weather
    Public SampleTimeStamp AS Date
    Public Temperature AS Double
    Public Humidity As Double
    Public WindSpeed AS Double
    Public WindBearing AS Double
End Class

Sub Main
    Dim samples = ReadFile("c://temp/DailyAug12log.csv")

    Dim avgTemperature = samples.Average(Function(s) s.Temperature)
    ...
End Sub

Private Function ReadFile(ByVal fileName as String) AS List(Of Weather)
    Dim samples As New List(Of Weather)
    Using tfp As new TextFieldParser(filename)
        tfp.Delimiters = new String() { "," }
        tfp.TextFieldType = FieldType.Delimited

        While Not tfp.EndOfData
            Dim fields = tfp.ReadFields()
            Dim sample As New Weather()

            sample.SampleTimeStamp = Date.ParseExact(fields(0) & fields(1), "dd\/MM\/yyyyHH\:mm", CultureInfo.InvariantCulture)
            sample.Temperature = Double.Parse(fields(2), CultureInfo.InvariantCulture)
            sample.Humidity = Double.Parse(fields(3), CultureInfo.InvariantCulture)
            sample.WindSpeed = Double.Parse(fields(4), CultureInfo.InvariantCulture)
            sample.WindBearing = Double.Parse(fields(5), CultureInfo.InvariantCulture)
            samples.Add(sample)
        End While

        Return samples
    End Using
End Function    

【讨论】:

  • 谢谢!我如何将每个的平均值显示到文本框中? :)
  • TextBox.Text = String.Format(CultureInfo.CurrentCulture, "平均温度为 {0}", avgTemperature)
【解决方案2】:

我不会使用这种方法 - 如果列的顺序发生变化,您的程序将显示错误的结果。 我会使用像 http://kbcsv.codeplex.com/ 这样的好的 csv 阅读器,并将数据读取到数据表中。那么你可以很容易和可靠地计算你的结果列,因为你可以像 MyDatatable.Cooumns["Headername"] 一样对每一列进行寻址。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-10
    • 1970-01-01
    • 2013-01-23
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-09-29
    相关资源
    最近更新 更多