|
The only answer I can give is maybe Email me the details and I will let you know.
|
|
|
|
|
The csv file which I want to import starts with some commentlines, but there are NOT marked as comment.
On the 3th line, the header starts and after the datalines. How I can skip this 2 lines ?
example of my csv:
Some text some text
Some text some test
ID; LastName, FirstName
1; Meier; Hans
2; Huber; Peter
3; Berger; Marcel
|
|
|
|
|
You can simply call ReadLine() on the underlying TextReader before starting to read with the CsvReader.
using (var reader = new StreamReader(...))
using (var csv = new CsvReader(reader, ...))
{
reader.ReadLine();
reader.ReadLine();
reader.ReadLine();
while (csv.ReadNextRecord())
{
}
}
|
|
|
|
|
Thanks a lot, it works fine.
I like the CSV-Reader - super work!
|
|
|
|
|
Greetings,
First off, this class is awesome!
I'm getting the following error when using some csv files (see sample content below) that we receive each week from another vendor:
LumenWorks.Framework.IO.Csv.MissingFieldCsvException: The CSV appears to be corrupt near record '0' field '1 at position '0'. Current raw data : ''
I'm using the class in an ASP.NET VB app.
The error is thrown when the reader hits the trailing arrow character () which according to this ASCII Character Code Reference is an ASCII Substitute character used as an end-of-file indicator for text files.
It's other references are:
ASCII Mnemonic SUB, Decimal 26, Binary 0b0011010, Hexadecimal 0x1A, Control Key CTRL-Z, CHAR(26)
Any suggestions on how to handle this end of file indicator? Maybe a way to trim/replace it on the fly??? Or halt reader execution?
Kind Regards,
Ben
sample csv content:
<BeginCsvContent>
ITEMCODE,SERIAL NUMBER,MFR MDL CODE,ENG MFR MDL,YEAR MFR,TYPE SIGNER,NAME,STREET,STREET2,CITY,STATE,ZIP CODE,REGION,COUNTY,COUNTRY,LAST ACTION DATE,ACQ DATE,CORE,TYPE ASSET,TYPE PULLY,STATUS CODE,MODE S CODE,BID OWNER,SOLD DATE ,OTHER-DESC(1) ,OTHER-DESC(2) ,OTHER-DESC(3) ,OTHER-DESC(4) ,OTHER-DESC(5) ,
1 ,1071 ,3980115,54556,1988,5,THE CO OF FR ,NATL JUMP ZONE,534 NW 43 ,OKLA CITY ,OK,73127,2,109,US,54677,7987987,1T ,5,5,A ,50000001, ,19880909, , , , , ,
1A ,4116 ,3870220,17025,1971,3,SPICES INTL ,1144 S ARK ST , ,AKRON ,OH,44316-1000,C,153,US,20061121,20031119,1N ,3,1,A ,50000002, ,20010228, , , , , ,
</EndCsvContent>
|
|
|
|
|
If the csv file has no comments, you can specify '\0x1A' as the comment character and that would do the job .. Or another way is to set DefaultParseErrorAction to ParseErrorAction.AdvanceToNextLine or ParseErrorAction.RaiseEvent and handle the ParseError event. I guess I could ignore the CTRL-Z char in a future release.
|
|
|
|
|
Thanks for the info. I searched the FAQ's for "comment character" and found a post where someone asked how to change the default comment character and the answer was to specify it at construction time using one of the constructors.
If this is what I need to do, can you please elaborate on what is meant by "specify it at construction time"? Is this a setting or value located in one of the source .cs files, which then has to be recompiled into a newer .dll file after editing it?
Thanks again.
|
|
|
|
|
var csv = new CsvReader(myTextReader, true, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, '\0x1A', true)
{
}
|
|
|
|
|
Thanks... but unfortunately, I get the same error
MissingFieldCsvException: The CSV appears to be corrupt near record '3981' field '1 at position '0'. Current raw data : ''.
I tried:
Using csv As New CsvReader(New StreamReader(f), True, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, "\0x1A", True)
rptSampleData.DataSource = csv
rptSampleData.DataBind()
End Using
I can check into your other suggestions... setting the DefaultParseErrorAction to ParseErrorAction.AdvanceToNextLine or ParseErrorAction.RaiseEvent and handle the ParseError event.
|
|
|
|
|
If the 0x1A char is really the first on the line, then it should be skipped. In all other cases, you will need to use my other suggestions.
|
|
|
|
|
yo, check it... this did the trick:
Using csv2 As New CsvReader(New StreamReader(f), True, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, Chr(26), True)
For grins (to see if I could really crash the app and break something) I slapped Chr(26) in place of the "\0x1A" and whamo!
Much Thanks!!! This rocks!
|
|
|
|
|
Haha, well you had to tell me you are using VB.NET :P Glad it worked then.
|
|
|
|
|
Is it possible to create an additional parameter specifying row delimiter on future release? Thanks
Needy
|
|
|
|
|
You mean the new line character ? That one is a bit tricky because it can be \r\n, \r or \n. Since the reader needs to check if the current char is a new line character for every single char (except when inside a quoted field), I must be careful about performance there. Do you actually need to specify another char for record delimiter ?
|
|
|
|
|
Unfortunately I'm now having the same problem (MalformedCsvException: The CSV appears to be corrupt near record) when the reader encounters a field that has quotes around only part of the content, and if the first character for that field is a quote character.
I've constructed my CsvReader as follows and am not sure what to tweak:
Using csv As New CsvReader(New StreamReader(f), True, CsvReader.DefaultDelimiter, CsvReader.DefaultQuote, CsvReader.DefaultEscape, Chr(26), True)
End Using
The MalformedCsvException is thrown when it hits the "R" U in the 4th row of my csv file.
sample csv content:
<BeginCsvContent>
0511905,POOLW ,T-MODEL "A" ,4,1,1,1,01,001,CLASS 1,0085,
9955225,JIES ,"Z" ,4,1,1,1,01,002,CLASS 1,0000,
05228BQ,WILCO JUNY ,SUN-EXPERIMENTAL ,4,1,1,1,01,002,CLASS 1,0000,
05338AT,TRAVEL BUS ,"R" U ,4,1,1,1,01,001,CLASS 1,0085,
00441BR,EWR-ACE ,CE 1 ,4,1,1,1,01,001,CLASS 1,0082,
99129XX,UNKNOWN ,UNKNOWN ,4,1,1,1,01,002,CLASS 1,0112,
</EndCsvContent>
Fyi... the CsvReader will leave the "A" in the first row unchanged and the "Z" in the second row is changed simply to just a Z. So I think it normally handles quote characters with ease, but I'm stumped on this one.
Any suggestions?
Kind Regards.
|
|
|
|
|
The reader consider a field value starting with the specified quote character to be quoted and validates it accordingly, which in your case leads to an error since there is still data after the closing quote. If you do not need to support quoted fields (ie no delimiter inside field values) or you can use another char as the quote, then simply specify another quote char in the constructor instead of CsvReader.DefaultQuote (same way as with the comment char).
|
|
|
|
|
Thanks for sharing this and for answering questions.
I attempted to find out how to do it myself but the code just confuses me. Is it possible to make a quick edit to require 18 fields (instead of detecting field count automatically)?
I've done some searching and I believe you said it was not possible to change the reader to throw an error when there are more than fields than the field count (18 in my case), and it can only catch an error if there is less. I just wanted to verify if what I think is true.
Thanks again +5
|
|
|
|
|
In the current version, you cannot require a set number of fields. If you want more flexibility, you can get my new library on CodePlex named NLight[^]. The solution and project files are for VS2010/.NET 4.0, but converting it back to .NET 2.0 will require minimal efforts as I just upgraded it.
|
|
|
|
|
The attached source code is not running.
|
|
|
|
|
|
I want if the field is blank then that complete record row should be fetched and in a method, it should get displayed on console.
Suppose, csv headers are : id, name, age and values are :
"1",,"29"
Then csv readre should get the current position of the record and fetch that record and display along with "name" is blank.
I tried with code snippet in error custom handling, as below:
csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent;
csv.ParseError += new
ParseErrorEventHandler (csv_ParseError);
but here ParseErrorEventHandler is missing namespace.
Please reply what should be done.
|
|
|
|
|
There is no ParseErrorEventHandler defined, you need to use EventHandler<ParseErrorEventArgs> or even better, simply write
csv.ParseError += csv_ParseError;
As for writing the record line, please check previous comment made only a few days ago.
|
|
|
|
|
Thanks for the reply. Worked for me.
But as you said, "As for writing the record line, please check previous comment made only a few days ago. ", I have checked and did not found, how to extract the record at which the error occured..
I tried with copyCurrentRecordTo() and that provides the data in array. I want to write this data into a new CSV file. How can I do that?
|
|
|
|
|
|
I want if the field is blank then that complete record row should be fetched and in a method, it should get displayed on console.
Suppose, csv headers are : id, name, age and values are :
"1",,"29"
Then csv readre should get the current position of the record and fetch that record and display along with "name" is blank.
I tried with code snippet in error custom handling, as below:
csv.DefaultParseErrorAction = ParseErrorAction.RaiseEvent;
csv.ParseError += new
ParseErrorEventHandler (csv_ParseError);
but here ParseErrorEventHandler is missing namespace.
Please reply what should be done.
|
|
|
|
|