-
May 14th, 2010, 09:03 AM
#1
[RESOLVED] String.Split
I'm having problems with parsing report data I get from 3rd party service.
The data is delivered in CSV format
Code:
Netherlands,5124.15,some more values
United States,102.10,some more values
using the String.Split method (and comma as seperator), reading this is not a problem.
But now I found the data like this
Code:
Netherlands,5124.15,some more values
"Congo, the Replublic of",123.45,some more
Parsing the second line result that the country is split into 2.
How can I prevent this? Or should I use regex instead of .Split method?
Things different in the second line:
- The name of the country is included with quotes
- After the comma in the country name is a space, between the values there's not.
-
May 14th, 2010, 10:07 AM
#2
Re: String.Split
I've always taken the csv reader approach...
http://www.codeproject.com/KB/database/CsvReader.aspx
From the article intro:
One would imagine that parsing CSV files is a straightforward and boring task. I was thinking that too, until I had to parse several CSV files of a couple GB each. After trying to use the OLEDB JET driver and various Regular Expressions, I still ran into serious performance problems. At this point, I decided I would try the custom class option. I scoured the net for existing code, but finding a correct, fast, and efficient CSV parser and reader is not so simple, whatever platform/language you fancy.
I say correct in the sense that many implementations merely use some splitting method like String.Split(). This will, obviously, not handle field values with commas. Better implementations may care about escaped quotes, trimming spaces before and after fields, etc., but none I found were doing it all, and more importantly, in a fast and efficient manner.
And, this led to the CSV reader class I present in this article. Its design is based on the System.IO.StreamReader class, and so is a non-cached, forward-only reader (similar to what is sometimes called a fire-hose cursor).
-
May 18th, 2010, 03:39 AM
#3
Re: String.Split
Sorry for my late respone.
The CsvReader seems to works :-)
Thanks
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|