|
-
November 1st, 2006, 03:32 PM
#1
Reading a CSV
I have a CSV file that has a "data", "more data" format. I have found how to use getline to read it in. Which works fine, and I just remove the "s. The problem is that I have found that the lines has commas in the data, ie "data1, data2", "more data" so it causing a problem when I read it in.
How can I read the data in better? Or have it read the lines by "", ?
Thanks
-
November 1st, 2006, 03:48 PM
#2
Re: Reading a CSV
Several solutions:
- Search the internet to find and use an already written CSV class reader.
- Split each line before removing the " as follows: find 2 consecutive " characters and extract all data between the 2 " and store it in an array of some sort. Pay attention to the way a " character is encoded when it appears inside the data itself, probably it is escaped like \" or something.
-
November 1st, 2006, 06:40 PM
#3
Re: Reading a CSV
Found that boost has a solution. Here is the info incase anyone else runs into it. I am just giving the jist of it.
Code:
#include <boost/tokenizer.hpp>
...
while (getline(in,line,'\n')) { //Read the file line by line
boost::tokenizer<boost::escaped_list_separator<char> > tok(line);
for(boost::tokenizer<boost::escaped_list_separator<char> >::iterator beg=tok.begin(); beg!=tok.end();++beg){
cout << *beg;
}
}
-
November 2nd, 2006, 09:15 AM
#4
Re: Reading a CSV
The simplest way (if you write your own) is to load a line at a time from the file.
Then read the line a character at a time.
If you encounter a quotes character " then your mode toggles.
When you are reading in normal mode then a , indicates a new token. When you are reading in literal mode (having encountered a quote character) then the comma is part of the token.
Not sure if CSV allows quotes to be part of the actual text. In parsing text that does, this would normally be achieved by an escape sequence. An escape sequence is generally 2 characters. (Note that in XML escape sequences begin with a & character and end with a semi-colon. HTML uses the same sequences).
You should be prepared for potential parse errors.
-
November 2nd, 2006, 06:11 PM
#5
Re: Reading a CSV
What I posted works 100%
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
|