September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
73
Task/CSV-data-manipulation/Aime/csv-data-manipulation.aime
Normal file
73
Task/CSV-data-manipulation/Aime/csv-data-manipulation.aime
Normal file
|
|
@ -0,0 +1,73 @@
|
|||
void
|
||||
read_csv(list t, text path)
|
||||
{
|
||||
file f;
|
||||
list l;
|
||||
|
||||
f_affix(f, path);
|
||||
while (f_news(f, l, 0, 0, ",") ^ -1) {
|
||||
l_append(t, l);
|
||||
}
|
||||
}
|
||||
|
||||
list
|
||||
sum_columns(list t)
|
||||
{
|
||||
list c, l;
|
||||
integer i;
|
||||
|
||||
l_append(c, "SUM");
|
||||
for (i, l in t) {
|
||||
if (i) {
|
||||
integer j, sum;
|
||||
text s;
|
||||
|
||||
sum = 0;
|
||||
for (j, s in l) {
|
||||
sum += atoi(s);
|
||||
}
|
||||
|
||||
l_append(c, sum);
|
||||
}
|
||||
}
|
||||
|
||||
return c;
|
||||
}
|
||||
|
||||
void
|
||||
add_column(list t, list c)
|
||||
{
|
||||
integer i;
|
||||
list l;
|
||||
|
||||
for (i, l in t) {
|
||||
l_append(l, c[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
write_csv(list t, text path)
|
||||
{
|
||||
integer i;
|
||||
file f;
|
||||
list l;
|
||||
|
||||
f_create(f, path, 00644);
|
||||
for (i, l in t) {
|
||||
f_(f, l[0]);
|
||||
l_ocall(l, f_, 2, 1, -1, f, ",");
|
||||
f_newline(f);
|
||||
}
|
||||
}
|
||||
|
||||
integer
|
||||
main(void)
|
||||
{
|
||||
list t;
|
||||
|
||||
read_csv(t, "tmp/CSV_data_manipulation.csv");
|
||||
add_column(t, sum_columns(t));
|
||||
write_csv(t, "tmp/CSV_data_manipulated.csv");
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
|
@ -0,0 +1,32 @@
|
|||
Public Sub Form_Open()
|
||||
Dim sData As String = File.Load("data.csv")
|
||||
Dim sLine, sTemp As String
|
||||
Dim sOutput As New String[]
|
||||
Dim siCount As Short
|
||||
Dim bLine1 As Boolean
|
||||
|
||||
For Each sLine In Split(sData, gb.NewLine)
|
||||
If Not bLine1 Then
|
||||
sLine &= ",SUM"
|
||||
sOutput.Add(sLine)
|
||||
bLine1 = True
|
||||
Continue
|
||||
End If
|
||||
For Each sTemp In Split(sLine)
|
||||
siCount += Val(sTemp)
|
||||
Next
|
||||
sOutput.Add(sLine & "," & Str(siCount))
|
||||
siCount = 0
|
||||
Next
|
||||
|
||||
sData = ""
|
||||
|
||||
For Each sTemp In sOutput
|
||||
sData &= sTemp & gb.NewLine
|
||||
Print sTemp;
|
||||
Print
|
||||
Next
|
||||
|
||||
File.Save(User.home &/ "CSVData.csv", sData)
|
||||
|
||||
End
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
const fs = require('fs');
|
||||
|
||||
// formats for the data parameter in the function below: {col1: array | function, col2: array | function}
|
||||
|
||||
function addCols(path, data) {
|
||||
let csv = fs.readFileSync(path, 'utf8');
|
||||
csv = csv.split('\n').map(line => line.trim());
|
||||
let colNames = Object.keys(data);
|
||||
for (let i = 0; i < colNames.length; i++) {
|
||||
let c = colNames[i];
|
||||
if (typeof data[c] === 'function') {
|
||||
csv = csv.map((line, idx) => idx === 0
|
||||
? line + ',' + c
|
||||
: line + ',' + data[c](line, idx)
|
||||
);
|
||||
} else if (Array.isArray(data[c])) {
|
||||
csv = csv.map((line, idx) => idx === 0
|
||||
? line + ',' + c
|
||||
: line + ',' + data[c][idx - 1]
|
||||
);
|
||||
}
|
||||
}
|
||||
fs.createWriteStream(path, {
|
||||
flag: 'w',
|
||||
defaultEncoding: 'utf8'
|
||||
}).end(csv.join('\n'));
|
||||
}
|
||||
|
||||
addCols('test.csv', {
|
||||
sum: function (line, idx) {
|
||||
let s = 0;
|
||||
line = line.split(',').map(d => +(d.trim()));
|
||||
for (let i = 0; i < line.length; i++) {
|
||||
s += line[i];
|
||||
}
|
||||
return s;
|
||||
},
|
||||
id: function(line, idx) {
|
||||
return idx;
|
||||
}
|
||||
});
|
||||
|
|
@ -0,0 +1,14 @@
|
|||
// version 1.1.3
|
||||
|
||||
import java.io.File
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
val lines = File("example.csv").readLines().toMutableList()
|
||||
lines[0] += ",SUM"
|
||||
for (i in 1 until lines.size) {
|
||||
lines[i] += "," + lines[i].split(',').sumBy { it.toInt() }
|
||||
}
|
||||
val text = lines.joinToString("\n")
|
||||
File("example2.csv").writeText(text) // write to new file
|
||||
println(text) // print to console
|
||||
}
|
||||
|
|
@ -0,0 +1,45 @@
|
|||
program CSV_Data_Manipulation;
|
||||
uses Classes, SysUtils;
|
||||
|
||||
var s: string;
|
||||
ts: tStringList;
|
||||
inFile,
|
||||
outFile: Text;
|
||||
Sum: integer;
|
||||
Number: string;
|
||||
|
||||
begin
|
||||
|
||||
Assign(inFile,'input.csv');
|
||||
Reset(inFile);
|
||||
|
||||
Assign(outFile,'result.csv');
|
||||
Rewrite(outFile);
|
||||
|
||||
ts:=tStringList.Create;
|
||||
ts.StrictDelimiter:=True;
|
||||
|
||||
// Handle the header
|
||||
ReadLn(inFile,s); // Read a line from input file
|
||||
ts.CommaText:=s; // Split it to lines
|
||||
ts.Add('SUM'); // Add a line
|
||||
WriteLn(outFile,ts.CommaText); // Reassemble it with comma as delimiter
|
||||
|
||||
// Handle the data
|
||||
while not eof(inFile) do
|
||||
begin
|
||||
ReadLn(inFile,s);
|
||||
ts.CommaText:=s;
|
||||
|
||||
Sum:=0;
|
||||
for Number in ts do
|
||||
Sum+=StrToInt(Number);
|
||||
|
||||
ts.Add('%D',[Sum]);
|
||||
|
||||
writeln(outFile, ts.CommaText);
|
||||
end;
|
||||
Close(outFile);
|
||||
Close(inFile);
|
||||
ts.Free;
|
||||
end.
|
||||
22
Task/CSV-data-manipulation/Phix/csv-data-manipulation.phix
Normal file
22
Task/CSV-data-manipulation/Phix/csv-data-manipulation.phix
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
integer fn = open("test.csv","r")
|
||||
sequence lines = {}
|
||||
while 1 do
|
||||
object line = gets(fn)
|
||||
if atom(line) then exit end if
|
||||
lines = append(lines,split(trim(line),','))
|
||||
end while
|
||||
close(fn)
|
||||
lines[1] = join(lines[1],',')&",SUM"
|
||||
for i=2 to length(lines) do
|
||||
sequence s = lines[i]
|
||||
for j=1 to length(s) do
|
||||
{{s[j]}} = scanf(s[j],"%d")
|
||||
end for
|
||||
-- s[rand(length(s))] = rand(100) -- (if you like)
|
||||
lines[i] = sprintf("%d,%d,%d,%d,%d,%d",s&sum(s))
|
||||
end for
|
||||
lines = join(lines,'\n')
|
||||
fn = open("out.csv","w")
|
||||
puts(fn,lines)
|
||||
close(fn)
|
||||
puts(1,lines)
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
import delimited input.csv, clear
|
||||
replace c5=c3+c4
|
||||
drop if mod(c3,3)==0
|
||||
export delimited using output.csv, replace
|
||||
8
Task/CSV-data-manipulation/Zkl/csv-data-manipulation.zkl
Normal file
8
Task/CSV-data-manipulation/Zkl/csv-data-manipulation.zkl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
csvFile:=File("test.csv");
|
||||
header:=csvFile.readln().strip(); // remove trailing "\n" and leading white space
|
||||
listOfLines:=csvFile.pump(List,fcn(line){ line.strip().split(",").apply("toInt") });
|
||||
|
||||
newFile:=File("test2.csv","w");
|
||||
newFile.writeln(header + ",sum");
|
||||
listOfLines.pump(newFile.writeln,fcn(ns){ String(ns.concat(","),",",ns.sum()) });
|
||||
newFile.close();
|
||||
Loading…
Add table
Add a link
Reference in a new issue