はじめに
FlotとHighchartsでCSVファイルを元にグラフを描画したときに「jquery.csv.js」を使いました。
「jquery.csv.js」は、CSVファイルを読み込むためのjQueryのプラグインです。
ソースを見ると改行コードが固定されていたので、自分が使い易いように修正しました。
第2引数で改行コードを指定出来るようにしています。
省略した場合の改行コードはLF「\n」。
【追記 2021.05.26】
こちらを参考に修正すると動作すると思われる。
https://qiita.com/cnloni/items/5de03096a73c702b97ed
【追記 2012.12.28】
こっちの方がスマートで便利っぽい。
記事のリンクは切れていたが、ホームページは残っている。
https://tips.recatnap.info/laboratory/
【追記 2012.09.21】
ご指摘がありましたので「linedelim」を「lined」に修正。
URL修正、追加。
環境
- jQuery ver.1.8.2
- jQuery ver.1.7
使用方法の例
タブ区切り、改行コード「LF」のテキストファイルを読みます。
var csv = $.csv("\t", "\n")( data );
カンマ区切り、改行コード「LF」のテキストファイルを読みます。
var csv = $.csv()( data );
タブ区切り、改行コード「CR+LF」のテキストファイルを読みます。
var csv = $.csv("\t", "\r\n")( data );
タブ区切り、改行コード「CR」のテキストファイルを読みます。
var csv = $.csv("\t", "\r")( data );
jquery.csv.js
既にリンク切れ。
jquery-csv
GitHub - evanplaice/jquery-csv: A jQuery CSV parser plugin. Battle Tested | Optimized | 100% IETF RFC 4180 Complete
A jQuery CSV parser plugin. Battle Tested | Optimized | 100% IETF RFC 4180 Complete - evanplaice/jqu...
改行コード
改行コード - Wikipedia
差分
--- jquery.csv-org.js 2011-11-18 16:32:11.000000000 +0900
+++ jquery.csv-v1.js 2011-11-18 16:40:29.000000000 +0900
@@ -6,10 +6,11 @@
*/
;
jQuery.extend({
- csv: function(delim, quote, linedelim) {
+ csv: function(delim, lf, quote, lined) {
delim = typeof delim == "string" ? new RegExp( "[" + (delim || "," ) + "]" ) : typeof delim == "undefined" ? "," : delim;
+ lf = typeof delim == "string" ? new RegExp( "[" + (lf || "\n" ) + "]" ) : typeof lf == "undefined" ? "\n" : lf;
quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"' ) + "]" ) : typeof quote == "undefined" ? '"' : quote;
- lined = typeof lined == "string" ? new RegExp( "[" + (lined || "\r\n") + "]+") : typeof lined == "undefined" ? "\r\n" : lined;
+ lined = typeof lined == "string" ? new RegExp( "[" + (lined || lf) + "]+") : typeof lined == "undefined" ? lf : lined;
function splitline (v) {
// Split the line using the delimitor
改造版
「jquery.csv.js」のファイル名でUTF-8で保存。
第2引数で改行コード「\n」、「\r」、「\r\n」を指定します。
/* Usage:
* jQuery.csv()(csvtext) returns an array of arrays representing the CSV text.
* jQuery.csv("\t", "\r\n")(tsvtext) uses Tab as a delimiter (comma is the default)
* jQuery.csv("\t", "\r\n", "'")(tsvtext) uses a single quote as the quote character instead of double quotes
* jQuery.csv("\t", "\r\n", "'\"")(tsvtext) uses single & double quotes as the quote character
*/
;
jQuery.extend({
csv: function(delim, lf, quote, lined) {
delim = typeof delim == "string" ? new RegExp( "[" + (delim || "," ) + "]" ) : typeof delim == "undefined" ? "," : delim;
lf = typeof delim == "string" ? new RegExp( "[" + (lf || "\n" ) + "]" ) : typeof lf == "undefined" ? "\n" : lf;
quote = typeof quote == "string" ? new RegExp("^[" + (quote || '"' ) + "]" ) : typeof quote == "undefined" ? '"' : quote;
lined = typeof lined == "string" ? new RegExp( "[" + (lined || lf) + "]+") : typeof lined == "undefined" ? lf : lined;
function splitline (v) {
// Split the line using the delimitor
var arr = v.split(delim),
out = [], q;
for (var i=0, l=arr.length; i<l; i++) {
if (q = arr[i].match(quote)) {
for (j=i; j<l; j++) {
if (arr[j].charAt(arr[j].length-1) == q[0]) { break; }
}
var s = arr.slice(i,j+1).join(delim);
out.push(s.substr(1,s.length-2));
i = j;
}
else { out.push(arr[i]); }
}
return out;
}
return function(text) {
var lines = text.split(lined);
for (var i=0, l=lines.length; i<l; i++) {
lines[i] = splitline(lines[i]);
}
return lines;
};
}
});


Comments