下面是從 .CSV 檔案中讀取資料的代碼,該檔案有 3 列(不斷變化)。有時,當用戶在 Excel 中打開 .csv 并洗掉其中的一行時,該行會被讀取為空行或空格行,并以逗號分隔。
當通過代碼讀取相同的內容時,我得到“,”作為輸入,它作為空行添加到我的資料表中。我怎樣才能逃脫這個空白行?
Dim sreader As StreamReader
Dim sstring As String
Dim dt As DataTable
Dim counter as Integer
sreader = File.OpenText(Path.ToString) 'this path is path of the excel
While sreader.Peek <> -1
sstring = sreader.Readline()
If sstring <> " " then ' how can I check here that the string does not have any content in it except for the seperating commas
Dim str As String () = sstring.Split(",")
Dim rowdt As DataRow
rowdt = dt.NewRow()
For i As Integer = 0 To dt.Columns.count-1
rowdt(i) = str(i).ToString()
Next
dt.rows.Add(rowdt)
End if
Counter = counter 1
End While
我嘗試了一些東西。已在答案部分發布
這是我試過的
Dim sreader As StreamReader
Dim counter as Integer
Dim sstring As String
Dim dt As DataTable
sreader = File.OpenText(Path.ToString)
While sreader.Peek <> -1
sstring = sreader.Readline()
Dim no as integer = 0
For each str as String in sstring.Split(",")
If str.ToString.Trim = "" then
no = no 1
End If
Next
If no <> 3 then
Dim str As String () = sstring.Split(",")
Dim rowdt As DataRow
rowdt = dt.NewRow()
For i As Integer = 0 To dt.Columns.count-1
rowdt(i) = str(i).ToString()
Next
dt.rows.Add(rowdt)
End if
End if
counter = counter 1
End While
uj5u.com熱心網友回復:
讀取所有行,然后只處理逗號之間有任何值的行
Dim path = "filename.txt"
Dim dt As New DataTable()
dt.Columns.AddRange(
{
New DataColumn("Column1"), New DataColumn("Column2"),
New DataColumn("Column3"), New DataColumn("Column4"),
New DataColumn("Column5"), New DataColumn("Column6"),
New DataColumn("Column7"), New DataColumn("Column8"),
New DataColumn("Column9"), New DataColumn("Column10")
})
Dim sw As New Stopwatch()
sw.Start()
Dim lines = File.ReadAllLines(Path)
For Each line In lines
Dim split = line.Split({","c}, StringSplitOptions.None)
If split.Any(Function(s) Not String.IsNullOrWhiteSpace(s)) Then
Dim row = dt.NewRow()
For i As Integer = 0 To dt.Columns.Count - 1
row(i) = split(i).ToString()
Next
dt.Rows.Add(row)
End If
Next
sw.Stop()
Console.WriteLine($"Took {sw.ElapsedMilliseconds} ms")
Console.WriteLine($"Read {dt.Rows.Count()} rows")
經過測驗以解決性能問題
檔案內容 1024 行a,b,c,d,e,f,g,h,i,j和一些,,,,,,,,,混合的行,包括檔案的最后一行
檔案的最后10 行:
A,B,C,d,E,F,G,H,I,J
,A,B,C,d,E,F,G,H,I,J
,,,,,,,,,
,,, ,,,,,,
a,b,c,d,e,f,g,h,i,j
a,b,c,d,e,f,g,h,i,j
,,,,,, ,,,
a,b,c,d,e,f,g,h,i,j
a,b,c,d,e,f,g,h,i,j
,,,,,,,,,,
StopWatch 物件顯示讀取所有行需要 2 毫秒。并且在生成的 DataTable 中正好有 1024 行資料。處理器跳過沒有值的行
花了 2 毫秒
讀取 1024 行
uj5u.com熱心網友回復:
我試過拆分和檢查字串。希望它有效。
Dim sreader As StreamReader
Dim counter as Integer
Dim sstring As String
Dim dt As DataTable
sreader = File.OpenText(Path.ToString)
While sreader.Peek <> -1
sstring = sreader.Readline()
Dim no as integer = 0
For each str as String in sstring.Split(",")
If str.ToString.Trim = "" then
no = no 1
End If
Next
If no <> 3 then
Dim str As String () = sstring.Split(",")
Dim rowdt As DataRow
rowdt = dt.NewRow()
For i As Integer = 0 To dt.Columns.count-1
rowdt(i) = str(i).ToString()
Next
dt.rows.Add(rowdt)
End if
End if
counter = counter 1
End While
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/363007.html
標籤:网络
上一篇:如何將安裝在Windows10上的mingw編譯器鏈接到ubuntu20.04終端?
下一篇:ReactNative(組件`input`的查看配置getter回呼必須是一個函式(收到`undefined`)。確保以組件名稱開頭)
