-
December 15th, 2007, 12:16 PM
#1
problem in reading a csv file
Hi alll,
I have a problem in reading a csv file. My probelm is when i read a comma seperated text file into a csv file,
i got exta columns as there are commas between text. for example my text file is like this..........
/........................../
philipsco.ltd,usa,james
k.w.p. philipines,co.ltd, new jersey, Rosy
Samsung co,ltd, japan, rekhamithal
/....................../
when i read this file, comma betweem philipines,co.ltd is divided into two columns which has to be in one column.Pls help me.......i need it urgently
-
December 15th, 2007, 12:29 PM
#2
Re: problem in reading a csv file
Using my Crystal Ball I can tell that you are doing something wrong.
Seriously, go back ad re-read your post. Remember we have NO other kowledge about what you are doing. HOW do you expect any of us to guess???
TheCPUWizard is a registered trademark, all rights reserved. (If this post was helpful, please RATE it!)
2008, 2009,2010
In theory, there is no difference between theory and practice; in practice there is.
* Join the fight, refuse to respond to posts that contain code outside of [code] ... [/code] tags. See here for instructions
* How NOT to post a question here
* Of course you read this carefully before you posted
* Need homework help? Read this first
-
December 17th, 2007, 05:10 AM
#3
Re: problem in reading a csv file
hi cpuwiz,
i'm guessing that what krish meant was something like split the following string:
k.w.p. philipines,co.ltd, new jersey, Rosy
into this:
k.w.p. philipines,co.ltd
new jersey
Rosy
but sinces it's a CSV, it's splitting it into this:
k.w.p. philipines
co.ltd
new jersey
Rosy
i've been wrong before, though....
cheers!
...we get by with a little help from our friends...
-
December 17th, 2007, 05:32 AM
#4
Re: problem in reading a csv file
Your CSV Format isn't correct. in CSV files, text is usually stuffed between " marks. This prevents comma's in there from messing up your seperations. And " is Escaped by a 2nd ". Usually this is how CSV files are built up.
So your code has to check if a value starts with a ". If that's the case, it's a text, and should keep reading until it hits another ". Then check if there's another " after it, in which case, one is dropped and you're still in text reading mode, so you go to the next ".
When you find one that doesn't have a " after it, your text is completed and you can expect a , to switch to the next field.
There are many variants on this idea, using ; instead of , to seperate values, or . or tabs, etc. Sometimes the first row will contain the column names, sometimes it's raw data. And ofcourse, back in the days that CSV was invented, object oriented coding and such was practicly non-existant, so you had to rewrite the whole thing again from scratch if you had to do another kind of import engine. ( read: copy-paste-coding )
On Error Kill(User)
-
December 17th, 2007, 05:36 AM
#5
Re: problem in reading a csv file
however, there is something wrong with the input CSV file. AFAIK, text columns must be surrounded by double-quotes so that there would no confusions with commas..
"philipsco.ltd", "usa", "james"
"k.w.p. philipines,co.ltd", "new jersey", "Rosy"
"Samsung co,ltd", "japan", "rekhamithal"
Busy
-
December 17th, 2007, 01:17 PM
#6
Re: problem in reading a csv file
Thread1 is correct. According to the CSV specification, if there are commas within text then it should be surrounded by double quotes.
This code should correctly parse any line within a CSV file:
Code:
string[] Lines = Regex.Split(Line, ",(?=(?:[^\"]*\"[^\"]*\")*(?![^\"]*\"))");
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
|