简体   繁体   中英

Extract whole row/line using Lumenworks CSV Parser

How do I read the whole row while using LumenWorks CVS parser? So far am only able to read entry by entry but not the whole row. ie if a row is a,b,c,d,e,f , am able to pluck out each individual alphabet. However, I want to be able to read the whole row

Currently I have:

my_csv = New CsvReader(New StreamReader(file_path), False, ",", resetPoint)
field_count = my_csv.FieldCount
While my_csv.ReadNextRecord()
    'process the data here
    'This code will process each individual alphabet in one row

The above reads each individual alphabet. What I want is to have something like

row = my_csv.row

Is this option available or something similar?

'EDITED'

When you have basic VB programming skills like me, this is what you come up with to solve the problem

Dim my_string As String = ""
        For x As Integer = 0 To my_csv.FieldCount - 1
            my_string += my_csv(x).ToString.Trim + ","
        Next
        my_string = Mid(my_string, 1, Len(my_string) - 1)
        Return my_string

By all means use the code in the marked answer. Its super elegant!

I haven't found anything available, but this should work:

VB:

Dim rowFields = Enumerable.Range(0, my_csv.FieldCount).
   Select(Function(field) my_csv(CInt(my_csv.CurrentRecordIndex), field))
Dim line As String = String.Join(my_csv.Delimiter.ToString(), rowFields)

C#:

var rowFields = Enumerable.Range(0, my_csv.FieldCount)
    .Select(field => my_csv[(int)my_csv.CurrentRecordIndex, field]);
string line = string.Join(my_csv.Delimiter.ToString(), rowFields);

I found a method using the CopyCurrentRecordTo(array) method, which seems to be marginally faster (a few seconds) the wider (more columns) a file is:

C#:

string[] currentRow = new string[csv.FieldCount];
while (csv.ReadNextRecord())
{
  csv.CopyCurrentRecordTo(currentRow);
  var line = string.Join(csv.Delimiter.ToString(), currentRow);
}

VB (this is from Telerik converter, beware):

Dim currentRow As String() = New String(csv.FieldCount - 1) {}
While csv.ReadNextRecord()
    csv.CopyCurrentRecordTo(currentRow)
    Dim line = String.Join(csv.Delimiter.ToString(), currentRow)
End While

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM