2013-05-07 80 views
0

我有每個月多個的.csv文件當中去,如:從讀和操作.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 
+0

您需要另一個變量來跟蹤您讀過的行數。每行增加一個。然後用行數除「total」變量並用計算出的值更新文本框。你可以聲明其他「總」變量來總結其他測量結果...... – 2013-05-07 02:27:18

+0

感謝我的工作!任何想法如何添加此?謝謝! – yolad 2013-05-07 03:39:21

+0

嚴重嗎?...以與「總」變量相同的位置/方式聲明線的計數器。它將是Integer類型。將該變量在與累積總變量相同的地方增加1。你認爲你應該計算while循環內部還是外部的平均值?平均使用哪種數據類型?整數還是雙精度? – 2013-05-07 04:10:20

回答

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  
+0

謝謝!我將如何顯示每個文本框的平均值? :) – yolad 2013-05-07 07:21:41

+0

TextBox.Text = String.Format(CultureInfo.CurrentCulture,「平均溫度爲{0}」,avgTemperature) – adrianm 2013-05-07 09:27:45

0

我不會用這個aprroach - 如果的列的順序,你的程序會顯示錯誤的結果。 我會使用一個很好的csv閱讀器,如http://kbcsv.codeplex.com/,並將數據讀取到數據表。那麼你可以很容易和可靠地計算你的結果列,因爲你可以像MyDatatable.Cooumns [「Headername」]那樣對每列進行處理。