| Page 1 of 1 | [ 10 posts ] |
|
I have a csv file that I read like this:
Code: var myDataSource = new YAHOO.util.DataSource("myfilename.csv"); myDataSource.responseType = YAHOO.util.DataSource.TYPE_TEXT; myDataSource.responseSchema = { recordDelim: "\n", fieldDelim: ",", fields: myFields, metaFields: { totalRecords: myTotalRecords } }; It's a regular CSV file and in CSV you can put fields in quotes if they contain commas that are not separators, something like this Code: field1, field2, field3 "this is field1", "this is field2, it has comma inside", "this is field 3" But putting the quotes doesn't seem to help. Is there some way to make this work? |
Alberto SantiniYUI Contributor
|
Hello Roman.
I think you should pass to the datasource an url (not a file) providing csv data. In my code I added myDataSource.dataType = YAHOO.util.DataSource.TYPE_TEXT; Hope that helps, Alberto |
|
I provide the url to the file and it is being read, my problem is that the fields get misaligned if I have a comma in one of the fields.
The comma is the separator, so it doesn't work even if I put the field in the quotes. |
Alberto SantiniYUI Contributor
|
Hello Roman.
You may change the field delimeter, for instance, to '#'. Regards, IceBox |
|
Seems like this may be a bug in DataSource TYPE_TEXT. I looked at the source in DataSourceBase.js and the parseTextData method uses a simple JS ".split" string method which won't know about string delimeters. Not sure if others have encountered this.
I'm not real current on my Regex skills, but I think a "greedy-aware" Regex parsing may be written to overcome using the .split technique that could resolve this. I don't think a user should have to reformat a comma delimiter, especially if they don't control the data feed. Todd |
Alberto SantiniYUI Contributor
|
Hello Todd.
I agree, but it was, of course, a workaround. Another workaround, it would be tweaking the data in doBeforeCallback. For instance, myDatasource.doBeforeCallback = function (oRequest, oFullResp, oParsedResp) { // Remove the first result (i.e., the headers); oParsedResp.results.shift(); return oParsedResp; }; There it could be added a different parsing. Regards, Alberto Edit1: For instance, (I tested the split with the regex, but not the oParsedResp) Code: myDatasource.doBeforeCallback = function (oRequest, oFullResp, oParsedResp) { // '"a", "b,c,d", "e"'.split(/,(?!(?:[^",]|[^"],[^"])+")/); oParsedResp = oFullResp.split(/,(?!(?:[^",]|[^"],[^"])+")/); return oParsedResp; }; Edit2: This one is better: '"a","b,c, d","e"'.split(/,(?=(?:[^\"]*\"[^\"]*\")*[^\"]*$)/) Last edited by icebox on Mon Jan 31, 2011 1:46 pm, edited 1 time in total. |
|
Alberto, I haven't tested your Regex yet, but was thinking maybe it could be proposed to Roman for his application to place into his code as an overloaded replacement method for the parseTextData method of DataSource. This would be a workable "hack" potentially for him to get around his problem, assuming the Regex works properly.
Todd |
|
thank for the replies, I will try the workaround.
I don't want to change the separator for this application since the comma separated values file could be read by Excel without any change and I re-use this file for this reason. |
|
Tried it and it takes a very long time to execute (I get stuck script warning in Firefox). I use pagination, so the return result is not that big (but it has plenty of comma separators, I think that is causing performance degradation)
|
Alberto SantiniYUI Contributor
|
Hello Roman.
I worked out a tiny example, using another approach. I parsed the response, replacing commas outside the quotes with another delimeter and removing then the quotes. Of course, it is a bit primitive, but you may arrange the parsing for your needs. http://pastebin.com/3p3Jub7p Hope that helps, IceBox |
| Page 1 of 1 | [ 10 posts ] |
| You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum |
© 2006-2013 Yahoo! Inc. All rights reserved.
All code on this site is licensed under the BSD License unless stated otherwise.
About This Site · Security Contact Info
Powered by phpBB® Forum Software © phpBB Group