|
|
Hi Sebastien, I am new to c# so I am working my way through the code, I have one question: I need to load a dataset with data from a CSV file, can I do that your solution? If so, it would be great if you can show how I should do it.
Thanks,
Ashley
|
|
|
|
|
I have since found a lot of discussion in the comments on using the csv reader with datatables - sorry, I hadn't seen it before.
I am still struggling with getting the data table populated because it fits 4 column data into 2 columns. The csv data does not have column headers, and looks like this:
,"Report"
"Sales Channel 1"
"Sales"
,"2004","2005","2006"
"A",721566080.000000,707081280.000000,754787520.000000
"B",35836208.000000,27874150.000000,24252956.000000
"C",78945720.000000,74349744.000000,76166552.000000
"D",30386580.000000,38601984.000000,47140400.000000
"E",491753536.000000,476412544.000000,505892384.000000
I am using the following code to load the table:
DataTable tbl=new DataTable();
using (CsvReader csv=new CsvReader(new StreamReader(filePath + fileName), false))
{
tbl.Load(csv);
}
I would be grateful for any help.
|
|
|
|
|
Sorry for the delay, was far away from any computer What do you mean by
ashhorner wrote: I am still struggling with getting the data table populated because it fits 4 column data into 2 columns.
|
|
|
|
|
Hi, no probs, thanks for coming back to me.
My dataset has 4 columns per row (after the first few lines which just define the data spec). However, when I load the data into a datatable and bind that table to a datagrid the 4 columns of data per row are displayed as 2 rows of 2 columns, with the data for column 3 and 4 or a row being reported in the next row in column 1 and 2 - so I end up with twice as many rows and half as many columns as I am supposed to have.
Ashley
|
|
|
|
|
Hi,
I want to simply load the contents of the csv file into the System.Data.DataTable Class as shown in the code below.
using (CachedCsvReader csv = new CachedCsvReader(new StreamReader("File.csv"), false))
{
DataTable result = new DataTable();
result.Load(csv);
}
CachedCsvReader class inherits CsvReader which Implements IDataReader, so I was expecting the code above to work.
But it throws an InvalidOperationExpression, with the message "No Current Record".
SJ
|
|
|
|
|
This is really great! Like most people, I started with the string.Split() method, and quickly found out that it didn't get everything... This was a very nice code, and I'm glad that I found it! It helped me out tremendously!! Thanks!
|
|
|
|
|
Thanks for taking the time to tell me your appreciation
|
|
|
|
|
Hi Sebastien,
I love this piece of code, it saved me so much time and it is very well written. I love the detailed exceptions it throws because I can catch them and tell my users exactly where their csv file is screwed up. Nice Job!
I did possibly find one very small bug. When a user accidentally includes two identical headers GetFieldHeaders() fails with a System.ArgumentException "An item with the same key has already been added." I was thinking you may want to catch this and throw a MalformedCsvException.
Anyway, just my 2 cents, thanks again for the code.
Isn't it nice how you only hear from users when they report bugs? ha ha
Regards,
Mike
|
|
|
|
|
From my point of view, the CSV is well formed, but with a logical error due to duplicate names in the header. I agree I could handle this case and throw a more appropriate exception. In next version
|
|
|
|
|
I've got a CSV file that was generated from an Excel file in the following format:
Date,Engine,Remote Campaign,AdGroup,Keyword,Contextual,Impressions,Clicks,Media Cost,PageViews : Count,Visits : Count
26-Feb-07,Yahoo,USA Today - Tech Evergreen,Default,Default,No,0,0,$0.00 ,,
27-Feb-07,Yahoo,USA Today - Tech Evergreen,Default,Default,No,0,0,$0.00 ,,
28-Feb-07,Yahoo,USA Today - Tech Evergreen,Default,Default,No,0,0,$0.00 ,,
1-Mar-07,Yahoo,USA Today - Tech Evergreen,Default,Default,No,0,0,$0.00 ,,
2-Mar-07,Yahoo,USA Today - Tech Evergreen,Default,Default,No,0,0,$0.00 ,,
3-Mar-07,Yahoo,USA Today - Tech Evergreen,Default,Default,No,0,0,$0.00 ,,
4-Mar-07,Yahoo,USA Today - Tech Evergreen,Default,Default,No,0,0,$0.00 ,,
26-Feb-07,Google,USATODAY_Tech,blogmaney,tech blogger,No,3,0,$0.00 ,,
27-Feb-07,Google,USATODAY_Tech,blogmaney,tech blogger,No,2,0,$0.00 ,,
28-Feb-07,Google,USATODAY_Tech,blogmaney,tech blogger,No,2,0,$0.00 ,,
1-Mar-07,Google,USATODAY_Tech,blogmaney,tech blogger,No,2,0,$0.00 ,,
2-Mar-07,Google,USATODAY_Tech,blogmaney,tech blogger,No,6,0,$0.00 ,,
3-Mar-07,Google,USATODAY_Tech,blogmaney,tech blogger,No,3,0,$0.00 ,,
4-Mar-07,Google,USATODAY_Tech,blogmaney,tech blogger,No,3,0,$0.00 ,,
27-Feb-07,Google,USATODAY_Tech,blogmaney,technology blog,No,0,0,$0.00 ,,
28-Feb-07,Google,USATODAY_Tech,blogmaney,technology blog,No,1,0,$0.00
2-Mar-07,Google,USATODAY_Tech,blogmaney,technology blog,No,2,0,$0.00
3-Mar-07,Google,USATODAY_Tech,blogmaney,technology blog,No,1,0,$0.00
4-Mar-07,Google,USATODAY_Tech,blogmaney,technology blog,No,1,0,$0.00
Using the latest
_csvReader = new CsvReader(new StreamReader(fileName), true);<br />
_csvReader.MissingFieldAction = MissingFieldAction.ReplaceByEmpty;
The above file fails to parse correctly. Can you offer any insight?
|
|
|
|
|
Hi, I use this code to read a lines from csv file:
<br />
using (CsvReader csv = new CsvReader(new StringReader(fFileName), false))<br />
<br />
int fieldCount = csv.FieldCount;<br />
string[] headers = csv.GetFieldHeaders();<br />
string cHeader = "";<br />
<br />
while (csv.ReadNextRecord())<br />
for (int i = 0; i < fieldCount; i++)<br />
{<br />
string s = csv[i];<br />
}<br />
}<br />
but don't work.
Using the csv file, called "test3.csv", do not come visualized the lines with the program demo.
It appears, of the dataGridView, only the written one "C:\\test3.csv". How I can do?
Thanks...
|
|
|
|
|
Please use the CachedCsvReader class for databinding purposes.
|
|
|
|
|
Hello,
I tried my luck to process a 25 column CSV file. Sadly without success. The run was interrupted because the program cannot agree that the mentioned dimension of "_fieldHeaderIndexes" in CsvReader.cs should be increased over value "10". The critical code is (line 1456 - 1462):
for (int i = 0; i < _fields.Length; i++)
{
_fieldHeaders[i] = _fields[i];
_fieldHeaderIndexes.Add(_fields[i], i);
}
What can I do to solve the problem?
The first lines of the CSV file are:
Company,Symbol,prop1,prop2,prop3,prop4,prop5,prop6,prop7,prop8,prop9,prop10,prop11,prop12,prop13,prop14,prop15,prop16,prop17,prop18,prop19,prop20,prop21,prop22,prop23
Pre Paid Legal Services,PPD,4496837,73.5,33.89,9.43,,,-14.44,14.44,-0.30,0.30,1.17,1.17,4377658,2.72,13267000,61200,14204958,$39.41,$559817393,NY,Services,Personal Services,234.89,2007-Jan
Holdrs Tr-Wireless,WMH,361996,69.6,,0.61,,,-6.98,6.98,4.38,4.38,3.46,3.46,370023,-2.17,,5200,1700000,$62.05,$105484999,AMX,,,,2007-Jan
Tasty Baking Company - Common Stock,TSTY,869314,68.2,,22.98,,,-10.78,10.78,1.56,1.56,-0.33,0.33,872018,-0.31,,12741,8215261,$9.00,$73937349,NAS,,,,2007-Jan
ZipRealty Inc - Common Stock,ZIPR,2294817,68.1,14.27,-13.91,,,-34.58,34.58,3.07,3.07,-1.34,1.34,2328538,-1.45,16080000,33686,21021021,$7.49,$157447442,NAS,Financial,Real Estate Development,-135.18,2007-Jan
Best regards
Michael
|
|
|
|
|
I am trying to implement the example above, including exception handling. Unfortunately I get an error on the following line:
csv.ParseError += new ParseErrorEventHandler(csv_ParseError);
Error:
Error 3 The type or namespace name 'ParseErrorEventHandler' could not be found (are you missing a using directive or an assembly reference?)
I am new to C# and would appreciate your help.
Regards,
Pieter
|
|
|
|
|
If you use .NET 2.0, the syntax is:
csv.ParseError += new EventHandler<ParseErrorEventArgs>(csv_ParseError);
or simply:
csv.ParseError += csv_ParseError;
If you use .NET 1.1 and you get this error, then please make sure you are referencing the CsvReader library (LumenWorks.Framework.IO.dll) correctly.
|
|
|
|
|
Thank you for your response.
The syntax error has now been resolved. Unfortunately I cannot seem to get the event handling working correctly: I would like to load a CSV file, e.g.:
a,b,c,d
a,b,
a,b,c,d
a,b,c,d
a,b,
a,b,c,d
a,b,c,d
It should then be able to count all the errors. In the case above it should report 2 lines that's not correct.
I have found a previous entry which seems to address a similar issue: http://www.codeproject.com/cs/database/CsvReader.asp?msg=1628796&searchkw=AdvanceToNextLine&sd=26+Dec+2004&ed=26+Mar+2007&stype=1#xx1628796xx, but I do not get the correct results.
I am really stuck and have tried everything.
Pieter
|
|
|
|
|
You should use MissingFieldAction property as follow:
csv.MissingFieldAction = MissingFieldAction.ReplaceByNull;
That way, when a field is null, that means it is missing.
|
|
|
|
|
Thank you for an excellent piece of code and kind help.
|
|
|
|
|
Can you give me some guidenance on how to adjust your code to allow it to read and write upper ascii values. Some of the Greek characters and other miscellaneous characters.?
Chuck
|
|
|
|
|
You need to specify the encoding of the stream. For example:
CsvReader csv = new CsvReader(new StreamReader(fileName, System.Text.Encoding.UTF8), true);
You might have to try other encodings until you get the exact one that you need to use.
|
|
|
|
|
I went with:
CsvReader csv = new CsvReader(new StreamReader(fileName, Encoding.GetEncoding(1252), true);
Once that was done your code is great.
Chuck
|
|
|
|
|
I downloaded the .NET 2.0 demo version, and it won't run because it is looking for a namespace called NUnit.
|
|
|
|
|
Either remove the test project from the solution or install the unit testing framework:
http://www.nunit.org/[^]
|
|
|
|
|
Just letting you know that you did a great job with this.
Cheers
My Blog
A human being should be able to change a diaper, plan an invasion, butcher a hog, conn a ship, design a building, write a sonnet, balance accounts, build a wall, set a bone, comfort the dying, take orders, give orders, cooperate, act alone, solve equations, analyze a new problem, pitch manure, program a computer, cook a tasty meal, fight efficiently, die gallantly. Specialization is for insects.
- -Lazarus Long
|
|
|
|
|