如何阅读特定的CSV单元格? [英] How to read specific CSV cell?

查看:88
本文介绍了如何阅读特定的CSV单元格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何阅读特定的CSV单元格文件?我使用CSVReader从CSV获取所有数据以便发送到数据库。



目前有一些csv文件从某些行开始。请参阅图像此处 [ ^ ]



我想获取单元格的数据A:4,A:5,A:6并且从A8开始读取所有值:D8向下。



如果我删除Row,我只会读完所有内容1-7



 私人  Sub  ReadCSVData( ByVal  filepathname  As  字符串
使用 csv 作为 CsvReader( StreamReader(文件路径名), True
Dim fieldCount 作为 整数 = csv.FieldCount
Dim headers()< span class =code-keyword> As String = csv.GetFieldHeaders()
Dim jsonData 作为 JArray()
Dim dt 作为 DataTable
执行 csv.ReadNextRecord()
Dim 记录作为 JObject()
对于 i 作为 整数 = 0 fieldCount - 1
record.Add( New JProperty(headers(i),csv(i) ))
下一步
循环
结束 使用
结束 Sub



现在真正的csv文件就像附加链接的图像



< b>我尝试了什么:



Private Sub ReadCSVData(ByVal filepathname As String)

使用csv作为新的CsvReader(新的StreamReader(filepathname),True)

Dim fieldCount As Integer = csv.FieldCount

Dim headers()As String = csv.GetFieldHeaders()

Dim jsonData As New JArray()

Dim dt As New DataTable

Do while csv.ReadNextRecord()

Dim record As New JObject()

For i As Integer = 0 to fieldCount - 1

record.Add(New JProperty(headers(i),csv(i)))

下一页

循环

结束使用

结束Sub

解决方案

< blockquote>假设你总是跳过前三行,并且CSV数据总是用空行与额外数据分开,这样的东西应该可以工作:

 专用 功能 ReadCSVData( ByVal  filepathname 作为 字符串作为 JObject 
Dim 结果作为 JObject( )

使用 sr 作为 新 StreamReader(filepathname)
' 跳过第一个三行:
sr.ReadLine()
sr.ReadLine()
sr.ReadLine()

' 从下一行获取额外数据:
Dim 作为 字符串 = sr.ReadLine()
执行直到 String .IsNullOrWhiteSpace(line)
Dim parts() As String = line.Split( c)
如果 parts.Length = 2 然后
result.Add( JProperty(parts( 0 )。修剪(),部分( 1 )。修剪()))
结束 如果

line = sr.ReadLine()
Loop

' 读取CSV数据:
< span class =code-keyword>使用 csv 作为 CsvReader(sr,< span class =code-keyword> True )
Dim fieldCount As 整数 = csv.FieldCount
Dim headers()作为 String = csv.GetFieldHeaders()
Dim jsonData As < span class =code-keyword> New JArray()

虽然 csv.ReadNextRecord()
Dim 记录 As JObject()
对于 i 作为 整数 = 0 fieldCount - 1
record.Add( New JProperty(headers(i),csv(i)))
下一步

jsonData.Add(记录)
循环

result.Add( JProperty( data,jsonData))
结束 使用
结束 使用

返回结果
结束 功能



输出:

 {
传感器ID ...
传感器描述 ...
状态 ...
data:[
{
时间 ...
传感器A < span class =code-string> ...

传感器B ...
传感器C ...
},
...
]
}


How to read specific CSV cell file? Im using CSVReader to get all data from CSV for sending to database purposes.

Currently there is some csv file that start from some line. Refer image here[^] for sample of this kind of csv.

I want to get data for cell A:4, A:5, A:6 and read all value start from A8:D8 downwards.

I'm only done for reading everything if I remove Row 1-7

Private Sub ReadCSVData(ByVal filepathname As String)
    Using csv As New CsvReader(New StreamReader(filepathname), True)
        Dim fieldCount As Integer = csv.FieldCount
        Dim headers() As String = csv.GetFieldHeaders()
        Dim jsonData As New JArray()
        Dim dt As New DataTable
        Do While csv.ReadNextRecord()
            Dim record As New JObject()
            For i As Integer = 0 To fieldCount - 1
                record.Add(New JProperty(headers(i), csv(i)))
            Next
        Loop
    End Using
End Sub


Now the real csv file is like attach link's image

What I have tried:

Private Sub ReadCSVData(ByVal filepathname As String)
Using csv As New CsvReader(New StreamReader(filepathname), True)
Dim fieldCount As Integer = csv.FieldCount
Dim headers() As String = csv.GetFieldHeaders()
Dim jsonData As New JArray()
Dim dt As New DataTable
Do While csv.ReadNextRecord()
Dim record As New JObject()
For i As Integer = 0 To fieldCount - 1
record.Add(New JProperty(headers(i), csv(i)))
Next
Loop
End Using
End Sub

解决方案

Assuming you're always skipping the first three lines, and the CSV data is always separated from the extra data by a blank line, something like this should work:

Private Function ReadCSVData(ByVal filepathname As String) As JObject
    Dim result As New JObject()
    
    Using sr As New StreamReader(filepathname)
        ' Skip the first three lines:
        sr.ReadLine()
        sr.ReadLine()
        sr.ReadLine()
        
        ' Grab the extra data from the next lines:
        Dim line As String = sr.ReadLine()
        Do Until String.IsNullOrWhiteSpace(line)
            Dim parts() As String = line.Split(":"c)
            If parts.Length = 2 Then
                result.Add(New JProperty(parts(0).Trim(), parts(1).Trim()))
            End If
            
            line = sr.ReadLine()
        Loop
        
        ' Read the CSV data:
        Using csv As New CsvReader(sr, True)
            Dim fieldCount As Integer = csv.FieldCount
            Dim headers() As String = csv.GetFieldHeaders()
            Dim jsonData As New JArray()
            
            Do While csv.ReadNextRecord()
                Dim record As New JObject()
                For i As Integer = 0 To fieldCount - 1
                    record.Add(New JProperty(headers(i), csv(i)))
                Next
                
                jsonData.Add(record)
            Loop
            
            result.Add(New JProperty("data", jsonData))
        End Using
    End Using
    
    Return result
End Function


Output:

{
  "Sensor ID": "...",
  "Sensor Description": "...",
  "Status": "...",
  "data": [
    {
      "Time": "...",
      "Sensor A": "...",
      "Sensor B": "...",
      "Sensor C": "..."
    },
    ...
  ]
}


这篇关于如何阅读特定的CSV单元格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆