mirror of https://github.com/adamdruppe/arsd.git
write csv function
This commit is contained in:
parent
abd79d2b74
commit
efd7d05bd1
22
csv.d
22
csv.d
|
@ -58,3 +58,25 @@ string[][] readCsv(string data) {
|
|||
|
||||
return records;
|
||||
}
|
||||
|
||||
///
|
||||
string toCsv(string[][] rows) {
|
||||
string data;
|
||||
|
||||
foreach(ridx, row; rows) {
|
||||
if(ridx) data ~= "\n";
|
||||
foreach(idx, cell; row) {
|
||||
if(idx) data ~= ",";
|
||||
|
||||
if(cell.indexOf(",") != -1 || cell.indexOf("\"") != -1 || cell.indexOf("\n") != -1) {
|
||||
data ~= "\"";
|
||||
data ~= cell.replace(`"`, `""`);
|
||||
data ~= "\"";
|
||||
} else {
|
||||
data ~= cell;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return data;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue