Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,2 @@
---
from: http://rosettacode.org/wiki/CSV_data_manipulation

View file

@ -0,0 +1,25 @@
[[wp:Comma-separated values|CSV spreadsheet files]] are suitable for storing tabular data in a relatively portable way.
The CSV format is flexible but somewhat ill-defined.
For present purposes, authors may assume that the data fields contain no commas, backslashes, or quotation marks.
;Task:
Read a CSV file, change some values and save the changes back to a file.
For this task we will use the following CSV file:
C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20
<i>Suggestions</i>
<ul>
<li/> Show how to add a column, headed 'SUM', of the sums of the rows.
<li/> If possible, illustrate the use of built-in or standard functions, methods, or libraries, that handle generic CSV files.
</ul>
<br><br>

View file

@ -0,0 +1,6 @@
L(=line) File(data.csv).read_lines()
I L.index == 0
line = ,SUM
E
line = ,sum(line.split(,).map(Int))
print(line)

View file

@ -0,0 +1,88 @@
# count occurrances of a char in string #
PROC char count = (CHAR c, STRING str) INT:
BEGIN
INT count := 0;
FOR i TO UPB str DO
IF c = str[i] THEN count +:= 1
FI
OD;
count
END;
# split string on separator #
PROC char split = (STRING str, CHAR sep) FLEX[]STRING :
BEGIN
INT strlen := UPB str, cnt := 0;
INT len, p;
INT start := 1;
[char count (sep, str) + 1] STRING list;
WHILE start <= strlen ANDF char in string (sep, p, str[start:]) DO
p +:= start - 1;
list[cnt +:= 1] := str[start:p-1];
start := p + 1
OD;
IF cnt = 0 THEN list[cnt +:= 1] := str
ELIF start <= UPB str + 1 THEN list[cnt +:= 1] := str[start:]
FI;
list
END;
PROC join = ([]STRING words, STRING sep) STRING:
IF UPB words > 0 THEN
STRING str := words [1];
FOR i FROM 2 TO UPB words DO
str +:= sep + words[i]
OD;
str
ELSE
""
FI;
# read a line from file #
PROC readline = (REF FILE f) STRING:
BEGIN
STRING line;
get (f, line); new line (f);
line
END;
# Add one item to tuple #
OP +:= = (REF FLEX[]STRING tuple, STRING item) VOID:
BEGIN
[UPB tuple+1]STRING new;
new[:UPB tuple] := tuple;
new[UPB new] := item;
tuple := new
END;
# convert signed number TO INT #
OP TOINT = (STRING str) INT:
BEGIN
INT n := 0, sign := 1;
FOR i TO UPB str WHILE sign /= 0 DO
IF is digit (str[i]) THEN n := n * 10 + ABS str[i] - ABS "0"
ELIF i = 1 AND str[i] = "-" THEN sign := -1
ELIF i /= 1 OR str[i] /= "+" THEN sign := 0
FI
OD;
n * sign
END;
OP STR = (INT i) STRING: whole (i,0);
# The main program #
FILE foo;
open (foo, "CSV_data_manipulation.data", stand in channel);
FLEX[0]STRING header := char split (readline (foo), ",");
header +:= "SUM";
print ((join (header, ","), new line));
WHILE NOT end of file (foo) DO
FLEX[0]STRING fields := char split (readline (foo), ",");
INT sum := 0;
FOR i TO UPB fields DO
sum +:= TOINT fields[i]
OD;
fields +:= STR sum;
print ((join (fields, ","), new line))
OD;
close (foo)

View file

@ -0,0 +1,13 @@
#!/usr/bin/awk -f
BEGIN { FS = OFS = "," }
NR==1 {
print $0, "SUM"
next
}
{
sum = 0
for (i=1; i<=NF; i++) {
sum += $i
}
print $0, sum
}

View file

@ -0,0 +1,19 @@
package CSV is
type Row(<>) is tagged private;
function Line(S: String; Separator: Character := ',') return Row;
function Next(R: in out Row) return Boolean;
-- if there is still an item in R, Next advances to it and returns True
function Item(R: Row) return String;
-- after calling R.Next i times, this returns the i'th item (if any)
private
type Row(Length: Natural) is tagged record
Str: String(1 .. Length);
Fst: Positive;
Lst: Natural;
Nxt: Positive;
Sep: Character;
end record;
end CSV;

View file

@ -0,0 +1,24 @@
package body CSV is
function Line(S: String; Separator: Character := ',')
return Row is
(Length => S'Length, Str => S,
Fst => S'First, Lst => S'Last, Nxt => S'First, Sep => Separator);
function Item(R: Row) return String is
(R.Str(R.Fst .. R.Lst));
function Next(R: in out Row) return Boolean is
Last: Natural := R.Nxt;
begin
R.Fst := R.Nxt;
while Last <= R.Str'Last and then R.Str(Last) /= R.Sep loop
-- find Separator
Last := Last + 1;
end loop;
R.Lst := Last - 1;
R.Nxt := Last + 1;
return (R.Fst <= R.Str'Last);
end Next;
end CSV;

View file

@ -0,0 +1,19 @@
with CSV, Ada.Text_IO; use Ada.Text_IO;
procedure CSV_Data_Manipulation is
Header: String := Get_Line;
begin
Put_Line(Header & ", SUM");
while not End_Of_File loop
declare
R: CSV.Row := CSV.Line(Get_Line);
Sum: Integer := 0;
begin
while R.Next loop
Sum := Sum + Integer'Value(R.Item);
Put(R.Item & ",");
end loop;
Put_Line(Integer'Image(Sum));
end;
end loop;
end CSV_Data_Manipulation;

View 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;
}

View file

@ -0,0 +1,23 @@
; data.csv
;
; C1,C2,C3,C4,C5
; 1,5,9,13,17
; 2,6,10,14,18
; 3,7,11,15,19
; 4,8,12,16,20
table: read.csv "data.csv"
data: []
loop table 'row [
addable: ["SUM"]
if row <> first table ->
addable: @[to :string sum map row 'x [to :integer x]]
'data ++ @[row ++ addable]
]
loop data 'row [
loop row 'column ->
prints pad column 6
print ""
]

View file

@ -0,0 +1,7 @@
Loop, Read, Data.csv
{
i := A_Index
Loop, Parse, A_LoopReadLine, CSV
Output .= (i=A_Index && i!=1 ? A_LoopField**2 : A_LoopField) (A_Index=5 ? "`n" : ",")
}
FileAppend, %Output%, NewData.csv

View file

@ -0,0 +1,14 @@
OPEN "manip.csv" FOR INPUT AS #1
OPEN "manip2.csv" FOR OUTPUT AS #2
LINE INPUT #1, header$
PRINT #2, header$ + ",SUM"
WHILE NOT EOF(1)
INPUT #1, c1, c2, c3, c4, c5
sum = c1 + c2 + c3 + c4 + c5
WRITE #2, c1, c2, c3, c4, c5, sum
WEND
CLOSE #1, #2
END

View file

@ -0,0 +1,7 @@
csvstr "C1,C2,C3,C4,C5", "1,5,9,13,17", "2,6,10,14,18", "3,7,11,15,19", "4,8,12,16,20"
Split (-˜¬×+`)=','
strdata >Split¨csvstr
intdata •BQN¨(1) strdata
sums "SUMS" +´˘ 1 intdata
done sums ˜˘ intdata

View file

@ -0,0 +1,19 @@
OPTION COLLAPSE TRUE
OPTION DELIM ","
csv$ = LOAD$("data.csv")
DOTIMES AMOUNT(csv$, NL$)
line$ = TOKEN$(csv$, _, NL$)
IF _ = 1 THEN
total$ = APPEND$(line$, 0, "SUM")
ELSE
line$ = CHANGE$(line$, _, STR$(_*10) )
total$ = APPEND$(total$, 0, line$, NL$)
total$ = APPEND$(total$, 0, STR$(LOOP(i, AMOUNT(line$), VAL(TOKEN$(line$, i)))) )
FI
DONE
SAVE total$ TO "data.csv"
PRINT total$

View file

@ -0,0 +1,129 @@
#include <map>
#include <vector>
#include <iostream>
#include <fstream>
#include <utility>
#include <functional>
#include <string>
#include <sstream>
#include <algorithm>
#include <cctype>
class CSV
{
public:
CSV(void) : m_nCols( 0 ), m_nRows( 0 )
{}
bool open( const char* filename, char delim = ',' )
{
std::ifstream file( filename );
clear();
if ( file.is_open() )
{
open( file, delim );
return true;
}
return false;
}
void open( std::istream& istream, char delim = ',' )
{
std::string line;
clear();
while ( std::getline( istream, line ) )
{
unsigned int nCol = 0;
std::istringstream lineStream(line);
std::string cell;
while( std::getline( lineStream, cell, delim ) )
{
m_oData[std::make_pair( nCol, m_nRows )] = trim( cell );
nCol++;
}
m_nCols = std::max( m_nCols, nCol );
m_nRows++;
}
}
bool save( const char* pFile, char delim = ',' )
{
std::ofstream ofile( pFile );
if ( ofile.is_open() )
{
save( ofile );
return true;
}
return false;
}
void save( std::ostream& ostream, char delim = ',' )
{
for ( unsigned int nRow = 0; nRow < m_nRows; nRow++ )
{
for ( unsigned int nCol = 0; nCol < m_nCols; nCol++ )
{
ostream << trim( m_oData[std::make_pair( nCol, nRow )] );
if ( (nCol+1) < m_nCols )
{
ostream << delim;
}
else
{
ostream << std::endl;
}
}
}
}
void clear()
{
m_oData.clear();
m_nRows = m_nCols = 0;
}
std::string& operator()( unsigned int nCol, unsigned int nRow )
{
m_nCols = std::max( m_nCols, nCol+1 );
m_nRows = std::max( m_nRows, nRow+1 );
return m_oData[std::make_pair(nCol, nRow)];
}
inline unsigned int GetRows() { return m_nRows; }
inline unsigned int GetCols() { return m_nCols; }
private:
// trim string for empty spaces in begining and at the end
inline std::string &trim(std::string &s)
{
s.erase(s.begin(), std::find_if(s.begin(), s.end(), std::not1(std::ptr_fun<int, int>(std::isspace))));
s.erase(std::find_if(s.rbegin(), s.rend(), std::not1(std::ptr_fun<int, int>(std::isspace))).base(), s.end());
return s;
}
private:
std::map<std::pair<unsigned int, unsigned int>, std::string> m_oData;
unsigned int m_nCols;
unsigned int m_nRows;
};
int main()
{
CSV oCSV;
oCSV.open( "test_in.csv" );
oCSV( 0, 0 ) = "Column0";
oCSV( 1, 1 ) = "100";
oCSV( 2, 2 ) = "200";
oCSV( 3, 3 ) = "300";
oCSV( 4, 4 ) = "400";
oCSV.save( "test_out.csv" );
return 0;
}

View file

@ -0,0 +1,21 @@
using System.IO;
using System.Linq;
namespace CSV_data_manipulation
{
class Program
{
static void Main()
{
var input = File.ReadAllLines("test_in.csv");
var output = input.Select((line, i) =>
{
if (i == 0)
return line + ",SUM";
var sum = line.Split(',').Select(int.Parse).Sum();
return line + "," + sum;
}).ToArray();
File.WriteAllLines("test_out.csv", output);
}
}
}

View file

@ -0,0 +1,275 @@
#define TITLE "CSV data manipulation"
#define URL "http://rosettacode.org/wiki/CSV_data_manipulation"
#define _GNU_SOURCE
#define bool int
#include <stdio.h>
#include <stdlib.h> /* malloc...*/
#include <string.h> /* strtok...*/
#include <ctype.h>
#include <errno.h>
/**
* How to read a CSV file ?
*/
typedef struct {
char * delim;
unsigned int rows;
unsigned int cols;
char ** table;
} CSV;
/**
* Utility function to trim whitespaces from left & right of a string
*/
int trim(char ** str) {
int trimmed;
int n;
int len;
len = strlen(*str);
n = len - 1;
/* from right */
while((n>=0) && isspace((*str)[n])) {
(*str)[n] = '\0';
trimmed += 1;
n--;
}
/* from left */
n = 0;
while((n < len) && (isspace((*str)[0]))) {
(*str)[0] = '\0';
*str = (*str)+1;
trimmed += 1;
n++;
}
return trimmed;
}
/**
* De-allocate csv structure
*/
int csv_destroy(CSV * csv) {
if (csv == NULL) { return 0; }
if (csv->table != NULL) { free(csv->table); }
if (csv->delim != NULL) { free(csv->delim); }
free(csv);
return 0;
}
/**
* Allocate memory for a CSV structure
*/
CSV * csv_create(unsigned int cols, unsigned int rows) {
CSV * csv;
csv = malloc(sizeof(CSV));
csv->rows = rows;
csv->cols = cols;
csv->delim = strdup(",");
csv->table = malloc(sizeof(char *) * cols * rows);
if (csv->table == NULL) { goto error; }
memset(csv->table, 0, sizeof(char *) * cols * rows);
return csv;
error:
csv_destroy(csv);
return NULL;
}
/**
* Get value in CSV table at COL, ROW
*/
char * csv_get(CSV * csv, unsigned int col, unsigned int row) {
unsigned int idx;
idx = col + (row * csv->cols);
return csv->table[idx];
}
/**
* Set value in CSV table at COL, ROW
*/
int csv_set(CSV * csv, unsigned int col, unsigned int row, char * value) {
unsigned int idx;
idx = col + (row * csv->cols);
csv->table[idx] = value;
return 0;
}
void csv_display(CSV * csv) {
int row, col;
char * content;
if ((csv->rows == 0) || (csv->cols==0)) {
printf("[Empty table]\n");
return ;
}
printf("\n[Table cols=%d rows=%d]\n", csv->cols, csv->rows);
for (row=0; row<csv->rows; row++) {
printf("[|");
for (col=0; col<csv->cols; col++) {
content = csv_get(csv, col, row);
printf("%s\t|", content);
}
printf("]\n");
}
printf("\n");
}
/**
* Resize CSV table
*/
int csv_resize(CSV * old_csv, unsigned int new_cols, unsigned int new_rows) {
unsigned int cur_col,
cur_row,
max_cols,
max_rows;
CSV * new_csv;
char * content;
bool in_old, in_new;
/* Build a new (fake) csv */
new_csv = csv_create(new_cols, new_rows);
if (new_csv == NULL) { goto error; }
new_csv->rows = new_rows;
new_csv->cols = new_cols;
max_cols = (new_cols > old_csv->cols)? new_cols : old_csv->cols;
max_rows = (new_rows > old_csv->rows)? new_rows : old_csv->rows;
for (cur_col=0; cur_col<max_cols; cur_col++) {
for (cur_row=0; cur_row<max_rows; cur_row++) {
in_old = (cur_col < old_csv->cols) && (cur_row < old_csv->rows);
in_new = (cur_col < new_csv->cols) && (cur_row < new_csv->rows);
if (in_old && in_new) {
/* re-link data */
content = csv_get(old_csv, cur_col, cur_row);
csv_set(new_csv, cur_col, cur_row, content);
} else if (in_old) {
/* destroy data */
content = csv_get(old_csv, cur_col, cur_row);
free(content);
} else { /* skip */ }
}
}
/* on rows */
free(old_csv->table);
old_csv->rows = new_rows;
old_csv->cols = new_cols;
old_csv->table = new_csv->table;
new_csv->table = NULL;
csv_destroy(new_csv);
return 0;
error:
printf("Unable to resize CSV table: error %d - %s\n", errno, strerror(errno));
return -1;
}
/**
* Open CSV file and load its content into provided CSV structure
**/
int csv_open(CSV * csv, char * filename) {
FILE * fp;
unsigned int m_rows;
unsigned int m_cols, cols;
char line[2048];
char * lineptr;
char * token;
fp = fopen(filename, "r");
if (fp == NULL) { goto error; }
m_rows = 0;
m_cols = 0;
while(fgets(line, sizeof(line), fp) != NULL) {
m_rows += 1;
cols = 0;
lineptr = line;
while ((token = strtok(lineptr, csv->delim)) != NULL) {
lineptr = NULL;
trim(&token);
cols += 1;
if (cols > m_cols) { m_cols = cols; }
csv_resize(csv, m_cols, m_rows);
csv_set(csv, cols-1, m_rows-1, strdup(token));
}
}
fclose(fp);
csv->rows = m_rows;
csv->cols = m_cols;
return 0;
error:
fclose(fp);
printf("Unable to open %s for reading.", filename);
return -1;
}
/**
* Open CSV file and save CSV structure content into it
**/
int csv_save(CSV * csv, char * filename) {
FILE * fp;
int row, col;
char * content;
fp = fopen(filename, "w");
for (row=0; row<csv->rows; row++) {
for (col=0; col<csv->cols; col++) {
content = csv_get(csv, col, row);
fprintf(fp, "%s%s", content,
((col == csv->cols-1) ? "" : csv->delim) );
}
fprintf(fp, "\n");
}
fclose(fp);
return 0;
}
/**
* Test
*/
int main(int argc, char ** argv) {
CSV * csv;
printf("%s\n%s\n\n",TITLE, URL);
csv = csv_create(0, 0);
csv_open(csv, "fixtures/csv-data-manipulation.csv");
csv_display(csv);
csv_set(csv, 0, 0, "Column0");
csv_set(csv, 1, 1, "100");
csv_set(csv, 2, 2, "200");
csv_set(csv, 3, 3, "300");
csv_set(csv, 4, 4, "400");
csv_display(csv);
csv_save(csv, "tmp/csv-data-manipulation.result.csv");
csv_destroy(csv);
return 0;
}

View file

@ -0,0 +1,66 @@
#include <gadget/gadget.h>
LIB_GADGET_START
void Muestra_archivo_original();
Main
if(Exist_file("load_matrix.txt"))
{
/* recupero informacion del archivo para su apertura segura */
F_STAT dataFile = Stat_file("load_matrix.txt");
if(dataFile.is_matrix) // tiene forma de matriz???
{
New multitype test;
/* establezco los rangos a leer */
Range for test [0:1:dataFile.total_lines-1, 0:1:dataFile.max_tokens_per_line-1];
/* cargamos el array con detección de enteros como LONG */
test = Load_matrix_mt( pSDS(test), "load_matrix.txt", dataFile, DET_LONG);
/* modifica algunas cosas del archivo */
/* sChg() no es necesario aquí, porque no se está cambiando el tipo
de la celda, sino que se reemplaza el string: */
///sChg( test, 0,1, "Columna 1");
/* Con Let() basta... */
Let( $s-test[0,1], "Columna 1");
$l-test[2,1] = 1000;
$l-test[2,2] = 2000;
/* inserto filas */
/* preparo la fila a insertar */
New multitype nueva_fila;
sAppend_mt(nueva_fila,"fila 3.1");
Append_mt(nueva_fila,float,0.0);
Append_mt(nueva_fila,int,0);
Append_mt(nueva_fila,double,0.0);
Append_mt(nueva_fila,long, 0L);
/* insertamos la misma fila en el array, 3 veces */
test = Insert_row_mt(pSDS(test),pSDS(nueva_fila), 4);
test = Insert_row_mt(pSDS(test),pSDS(nueva_fila), 4);
test = Insert_row_mt(pSDS(test),pSDS(nueva_fila), 4);
Free multitype nueva_fila;
Print "\nGuardando archivo en \"save_matrix.txt\"...\n";
DEC_PREC = 20; /* establece precision decimal para despliegue */
All range for test;
Save_matrix_mt(SDS(test), "save_matrix.txt" );
Free multitype test;
Print "\nArchivo original:\n";
Muestra_archivo_original();
}
}
End
void Muestra_archivo_original(){
String csys;
csys = `cat load_matrix.txt`;
Print "\n%s\n", csys;
Free secure csys;
}

View file

@ -0,0 +1,93 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. CSV.
AUTHOR. Bill Gunshannon.
INSTALLATION. Home.
DATE-WRITTEN. 19 December 2021.
************************************************************
** Program Abstract:
** CSVs are something COBOL does pretty well.
** The commented out CONCATENATE statements are a
** second method other than the STRING method.
************************************************************
ENVIRONMENT DIVISION.
CONFIGURATION SECTION.
REPOSITORY.
FUNCTION ALL INTRINSIC.
INPUT-OUTPUT SECTION.
FILE-CONTROL.
SELECT CSV-File ASSIGN TO "csv.txt"
ORGANIZATION IS LINE SEQUENTIAL.
SELECT Out-File ASSIGN TO "new.csv.txt"
ORGANIZATION IS LINE SEQUENTIAL.
DATA DIVISION.
FILE SECTION.
FD CSV-File
DATA RECORD IS CSV-Record.
01 CSV-Record.
05 Field1 PIC X(64).
FD Out-File
DATA RECORD IS Out-Line.
01 Out-Line PIC X(80).
WORKING-STORAGE SECTION.
01 Eof PIC X VALUE 'F'.
01 CSV-Data.
05 CSV-Col1 PIC 9(5).
05 CSV-Col2 PIC 9(5).
05 CSV-Col3 PIC 9(5).
05 CSV-Col4 PIC 9(5).
05 CSV-Col5 PIC 9(5).
01 CSV-Sum PIC ZZZ9.
01 CSV-Sum-Alpha
REDEFINES CSV-Sum PIC X(4).
PROCEDURE DIVISION.
Main-Program.
OPEN INPUT CSV-File
OPEN OUTPUT Out-File
PERFORM Read-a-Record
PERFORM Build-Header
PERFORM UNTIL Eof = 'T'
PERFORM Read-a-Record
IF Eof NOT EQUAL 'T' PERFORM Process-a-Record
END-PERFORM
CLOSE CSV-File
CLOSE Out-File
STOP RUN.
Read-a-Record.
READ CSV-File
AT END MOVE 'T' TO Eof
END-READ.
Build-Header.
** MOVE CONCATENATE(TRIM(CSV-Record), ",SUM"
** TO Out-Line.
STRING TRIM(CSV-Record), ",SUM" INTO Out-Line.
WRITE Out-Line.
MOVE SPACES TO Out-Line.
Process-a-Record.
UNSTRING CSV-Record DELIMITED BY ',' INTO
CSV-Col1 CSV-Col2 CSV-Col3 CSV-Col4 CSV-Col5.
COMPUTE CSV-Sum =
CSV-Col1 + CSV-Col2 + CSV-Col3 + CSV-Col4 + CSV-Col5.
** MOVE CONCATENATE(TRIM(CSV-Record), "," TRIM(CSV-Sum-Alpha))
** TO Out-Line.
STRING TRIM(CSV-Record), "," TRIM(CSV-Sum-Alpha)
INTO Out-Line.
WRITE Out-Line.
MOVE SPACES TO Out-Line.
END-PROGRAM.

View file

@ -0,0 +1,14 @@
(require '[clojure.data.csv :as csv]
'[clojure.java.io :as io])
(defn add-sum-column [coll]
(let [titles (first coll)
values (rest coll)]
(cons (conj titles "SUM")
(map #(conj % (reduce + (map read-string %))) values))))
(with-open [in-file (io/reader "test_in.csv")]
(doall
(let [out-data (add-sum-column (csv/read-csv in-file))]
(with-open [out-file (io/writer "test_out.csv")]
(csv/write-csv out-file out-data)))))

View file

@ -0,0 +1,10 @@
(require '[tech.v3.dataset :as ds]
'[tech.v3.datatype.functional :as dfn])
(defn add-sum
[dataframe]
(assoc dataframe
"SUM"
(apply dfn/+ (map dataframe (ds/column-names dataframe)))))
(ds/write! (add-sum (ds/->dataset "resources/input.csv")) "resources/output.csv")

View file

@ -0,0 +1,39 @@
(defun csvfile-to-nested-list (filename delim-char)
"Reads the csv to a nested list, where each sublist represents a line."
(with-open-file (input filename)
(loop :for line := (read-line input nil) :while line
:collect (read-from-string
(substitute #\SPACE delim-char
(format nil "(~a)~%" line))))))
(defun sublist-sum-list (nested-list)
"Return a list with the sum of each list of numbers in a nested list."
(mapcar (lambda (l) (if (every #'numberp l)
(reduce #'+ l) nil))
nested-list))
(defun append-each-sublist (nested-list1 nested-list2)
"Horizontally append the sublists in two nested lists. Used to add columns."
(mapcar #'append nested-list1 nested-list2))
(defun nested-list-to-csv (nested-list delim-string)
"Converts the nested list back into a csv-formatted string."
(format nil (concatenate 'string "~{~{~2,'0d" delim-string "~}~%~}")
nested-list))
(defun main ()
(let* ((csvfile-path #p"projekte/common-lisp/example_comma_csv.txt")
(result-path #p"results.txt")
(data-list (csvfile-to-nested-list csvfile-path #\,))
(list-of-sums (sublist-sum-list data-list))
(result-header "C1,C2,C3,C4,C5,SUM"))
(setf data-list ; add list of sums as additional column
(rest ; remove old header
(append-each-sublist data-list
(mapcar #'list list-of-sums))))
;; write to output-file
(with-open-file (output result-path :direction :output :if-exists :supersede)
(format output "~a~%~a"
result-header (nested-list-to-csv data-list ",")))))
(main)

View file

@ -0,0 +1,10 @@
void main() {
import std.stdio, std.csv, std.file, std.typecons, std.array,
std.algorithm, std.conv, std.range;
auto rows = "csv_data_in.csv".File.byLine;
auto fout = "csv_data_out.csv".File("w");
fout.writeln(rows.front);
fout.writef("%(%(%d,%)\n%)", rows.dropOne
.map!(r => r.csvReader!int.front.map!(x => x + 1)));
}

View file

@ -0,0 +1,46 @@
program CSV_data_manipulation;
{$APPTYPE CONSOLE}
uses
System.SysUtils,
System.IoUtils,
System.Types;
type
TStringDynArrayHelper = record helper for TStringDynArray
function Sum: Integer;
end;
{ TStringDynArrayHelper }
function TStringDynArrayHelper.Sum: Integer;
var
value: string;
begin
Result := 0;
for value in self do
Result := Result + StrToIntDef(value, 0);
end;
const
FILENAME = './Data.csv';
var
i: integer;
Input, Row: TStringDynArray;
begin
Input := TFile.ReadAllLines(FILENAME);
for i := 0 to High(Input) do
begin
if i = 0 then
Input[i] := Input[i] + ',SUM'
else
begin
Row := Input[i].Split([',']);
Input[i] := Input[i] + ',' + row.Sum.ToString;
end;
end;
TFile.WriteAllLines(FILENAME, Input);
end.

View file

@ -0,0 +1,21 @@
// Assumes a CSV file exists and has been sprayed to a Thor cluster
MyFileLayout := RECORD
STRING Field1;
STRING Field2;
STRING Field3;
STRING Field4;
STRING Field5;
END;
MyDataset := DATASET ('~Rosetta::myCSVFile', MyFileLayout,CSV(SEPARATOR(',')));
MyFileLayout Appended(MyFileLayout pInput):= TRANSFORM
SELF.Field1 := pInput.Field1 +'x';
SELF.Field2 := pInput.Field2 +'y';
SELF.Field3 := pInput.Field3 +'z';
SELF.Field4 := pInput.Field4 +'a';
SELF.Field5 := pInput.Field5 +'b';
END ;
MyNewDataset := PROJECT(MyDataset,Appended(LEFT));
OUTPUT(myNewDataset,,'~Rosetta::myNewCSVFile',CSV,OVERWRITE);

View file

@ -0,0 +1,21 @@
;; CSV -> LISTS
(define (csv->row line) (map (lambda(x) (or (string->number x) x)) (string-split line ",")))
(define (csv->table csv) (map csv->row (string-split csv "\n")))
;; LISTS -> CSV
(define (row->csv row) (string-join row ","))
(define (table->csv header rows)
(string-join (cons (row->csv header) (for/list ((row rows)) (row->csv row))) "\n"))
(define (task file)
(let*
((table (csv->table file))
(header (first table))
(rows (rest table)))
(table->csv
(append header "SUM") ;; add last column
(for/list ((row rows)) (append row (apply + row))))))

View file

@ -0,0 +1,15 @@
(define file.csv #<<
C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20
>>#)
(task file.csv)
→ "C1,C2,C3,C4,C5,SUM
1,5,9,13,17,45
2,6,10,14,18,50
3,7,11,15,19,55
4,8,12,16,20,60"

View file

@ -0,0 +1,51 @@
defmodule Csv do
defstruct header: "", data: "", separator: ","
def from_file(path) do
[header | data] = path
|> File.stream!
|> Enum.to_list
|> Enum.map(&String.trim/1)
%Csv{ header: header, data: data }
end
def sums_of_rows(csv) do
Enum.map(csv.data, fn (row) -> sum_of_row(row, csv.separator) end)
end
def sum_of_row(row, separator) do
row
|> String.split(separator)
|> Enum.map(&String.to_integer/1)
|> Enum.sum
|> to_string
end
def append_column(csv, column_header, column_data) do
header = append_to_row(csv.header, column_header, csv.separator)
data = [csv.data, column_data]
|> List.zip
|> Enum.map(fn ({ row, value }) ->
append_to_row(row, value, csv.separator)
end)
%Csv{ header: header, data: data }
end
def append_to_row(row, value, separator) do
row <> separator <> value
end
def to_file(csv, path) do
body = Enum.join([csv.header | csv.data], "\n")
File.write(path, body)
end
end
csv = Csv.from_file("in.csv")
csv
|> Csv.append_column("SUM", Csv.sums_of_rows(csv))
|> Csv.to_file("out.csv")

View file

@ -0,0 +1,36 @@
-module( csv_data ).
-export( [change/2, from_binary/1, from_file/1, into_file/2, task/0] ).
change( CSV, Changes ) -> lists:foldl( fun change_foldl/2, CSV, Changes ).
from_binary( Binary ) ->
Lines = binary:split( Binary, <<"\n">>, [global] ),
[binary:split(X, <<",">>, [global]) || X <- Lines].
from_file( Name ) ->
{ok, Binary} = file:read_file( Name ),
from_binary( Binary ).
into_file( Name, CSV ) ->
Binaries = join_binaries( [join_binaries(X, <<",">>) || X <- CSV], <<"\n">> ),
file:write_file( Name, Binaries ).
task() ->
CSV = from_file( "CSV_file.in" ),
New_CSV = change( CSV, [{2,3,<<"23">>}, {4,4,<<"44">>}] ),
into_file( "CSV_file.out", New_CSV ).
change_foldl( {Row_number, Column_number, New}, Acc ) ->
{Row_befores, [Row_columns | Row_afters]} = split( Row_number, Acc ),
{Column_befores, [_Old | Column_afters]} = split( Column_number, Row_columns ),
Row_befores ++ [Column_befores ++ [New | Column_afters]] ++ Row_afters.
join_binaries( Binaries, Binary ) ->
[_Last | Rest] = lists:reverse( lists:flatten([[X, Binary] || X <- Binaries]) ),
lists:reverse( Rest ).
split( 1, List ) -> {[], List};
split( N, List ) -> lists:split( N - 1, List ).

View file

@ -0,0 +1,91 @@
--- Read CSV file and add columns headed with 'SUM'
--- with trace
-- trace(0)
include get.e
include std/text.e
function split(sequence s, integer c)
sequence removables = " \t\n\r\x05\u0234\" "
sequence out
integer first, delim
out = {}
first = 1
while first <= length(s) do
delim = find_from(c,s,first)
if delim = 0 then
delim = length(s)+1
end if
out = append(out,trim(s[first..delim-1],removables))
first = delim + 1
end while
return out
end function
procedure main()
integer fn -- the file number
integer fn2 -- the output file number
integer e -- the number of lines read
object line -- the next line from the file
sequence data = {} -- parsed csv data row
sequence headerNames = {} -- array saving column names
atom sum = 0.0 -- sum for each row
sequence var -- holds numerical data read
-- First we try to open the file called "data.csv".
fn = open("data.csv", "r")
if fn = -1 then
puts(1, "Can't open data.csv\n")
-- abort();
end if
-- Then we create an output file for processed data.
fn2 = open("newdata.csv", "w")
if fn2 = -1 then
puts(1, "Can't create newdata.csv\n")
end if
-- By successfully opening the file we have established that
-- the file exists, and open() gives us a file number (or "handle")
-- that we can use to perform operations on the file.
e = 1
while 1 do
line = gets(fn)
if atom(line) then
exit
end if
data = split(line, ',')
if (e=1) then
-- Save the header labels and
-- write them to output file.
headerNames = data
for i=1 to length(headerNames) do
printf(fn2, "%s,", {headerNames[i]})
end for
printf(fn2, "SUM\n")
end if
-- Run a sum for the numerical data.
if (e >= 2) then
for i=1 to length(data) do
printf(fn2, "%s,", {data[i]})
var = value(data[i])
if var[1] = 0 then
-- data read is numerical
-- add to sum
sum = sum + var[2]
end if
end for
printf(fn2, "%g\n", {sum})
sum = 0.0
end if
e = e + 1
end while
close(fn)
close(fn2)
end procedure
main()

View file

@ -0,0 +1,14 @@
open System.IO
[<EntryPoint>]
let main _ =
let input = File.ReadAllLines "test_in.csv"
let output =
input
|> Array.mapi (fun i line ->
if i = 0 then line + ",SUM"
else
let sum = Array.sumBy int (line.Split(','))
sprintf "%s,%i" line sum)
File.WriteAllLines ("test_out.csv", output)
0

View file

@ -0,0 +1,10 @@
USING: csv io.encodings.utf8 kernel math.parser sequences ;
IN: rosetta-code.csv-manipulation
: append-sum ( seq -- seq' )
dup [ string>number ] map-sum number>string suffix ;
: csv-sums ( seq -- seq' )
[ 0 = [ "SUM" suffix ] [ append-sum ] if ] map-index ;
"example.csv" utf8 [ file>csv csv-sums ] [ csv>file ] 2bi

View file

@ -0,0 +1,49 @@
\ csvsum.fs Add a new column named SUM that contain sums from rows of CommaSeparatedValues
\ USAGE:
\ gforth-fast csvsum.fs -e "stdout stdin csvsum bye" <input.csv >output.csv
CHAR , CONSTANT SEPARATOR
3 CONSTANT DECIMALS
1E1 DECIMALS S>D D>F F** FCONSTANT FSCALE
: colsum ( ca u -- F: -- sum ;return SUM from CSV-string )
0E0 OVER SWAP BOUNDS
?DO ( a )
I C@ SEPARATOR =
IF ( a )
I TUCK OVER - >FLOAT IF F+ THEN
1+
THEN
LOOP DROP
;
: f>string ( -- ca u F: x -- )
FSCALE F*
F>D TUCK DABS <# DECIMALS 0 DO # LOOP [CHAR] . HOLD #S ROT SIGN #>
;
: rowC!+ ( offs char -- u+1 ;store CHAR at here+OFFS,increment offset )
OVER HERE + C! 1+
;
: row$!+ ( offs ca u -- offs+u ;store STRING at here+OFFS,update offset )
ROT 2DUP + >R HERE + SWAP MOVE R>
;
\ If run program with '-m 4G'option, we have practically 4G to store a row
: csvsum ( fo fi -- ;write into FILEID-OUTPUT processed input from FILEID-INPUT )
2DUP
HERE UNUSED ROT READ-LINE THROW
IF ( fo fi fo u )
HERE SWAP ( fo fi fo ca u )
SEPARATOR rowC!+
s\" SUM" row$!+ ( fo fi fo ca u' )
ROT WRITE-LINE THROW
BEGIN ( fo fi )
2DUP HERE UNUSED ROT READ-LINE THROW
WHILE ( fo fi fo u )
HERE SWAP ( fo fi fo ca u )
SEPARATOR rowC!+
HERE OVER colsum f>string ( fo fi fo ca u ca' u' )
row$!+ ( fo fi fo ca u'+u )
ROT WRITE-LINE THROW
REPEAT
THEN
2DROP 2DROP
;

View file

@ -0,0 +1,107 @@
program rowsum
implicit none
character(:), allocatable :: line, name, a(:)
character(20) :: fmt
double precision, allocatable :: v(:)
integer :: n, nrow, ncol, i
call get_command_argument(1, length=n)
allocate(character(n) :: name)
call get_command_argument(1, name)
open(unit=10, file=name, action="read", form="formatted", access="stream")
deallocate(name)
call get_command_argument(2, length=n)
allocate(character(n) :: name)
call get_command_argument(2, name)
open(unit=11, file=name, action="write", form="formatted", access="stream")
deallocate(name)
nrow = 0
ncol = 0
do while (readline(10, line))
nrow = nrow + 1
call split(line, a)
if (nrow == 1) then
ncol = size(a)
write(11, "(A)", advance="no") line
write(11, "(A)") ",Sum"
allocate(v(ncol + 1))
write(fmt, "('(',G0,'(G0,:,''',A,'''))')") ncol + 1, ","
else
if (size(a) /= ncol) then
print "(A,' ',G0)", "Invalid number of values on row", nrow
stop
end if
do i = 1, ncol
read(a(i), *) v(i)
end do
v(ncol + 1) = sum(v(1:ncol))
write(11, fmt) v
end if
end do
close(10)
close(11)
contains
function readline(unit, line)
use iso_fortran_env
logical :: readline
integer :: unit, ios, n
character(:), allocatable :: line
character(10) :: buffer
line = ""
readline = .false.
do
read(unit, "(A)", advance="no", size=n, iostat=ios) buffer
if (ios == iostat_end) return
readline = .true.
line = line // buffer(1:n)
if (ios == iostat_eor) return
end do
end function
subroutine split(line, array, separator)
character(*) line
character(:), allocatable :: array(:)
character, optional :: separator
character :: sep
integer :: n, m, p, i, k
if (present(separator)) then
sep = separator
else
sep = ","
end if
n = len(line)
m = 0
p = 1
k = 1
do i = 1, n
if (line(i:i) == sep) then
p = p + 1
m = max(m, i - k)
k = i + 1
end if
end do
m = max(m, n - k + 1)
if (allocated(array)) deallocate(array)
allocate(character(m) :: array(p))
p = 1
k = 1
do i = 1, n
if (line(i:i) == sep) then
array(p) = line(k:i-1)
p = p + 1
k = i + 1
end if
end do
array(p) = line(k:n)
end subroutine
end program

View file

@ -0,0 +1,20 @@
Copies a file with 5 comma-separated values to a line, appending a column holding their sum.
INTEGER N !Instead of littering the source with "5"
PARAMETER (N = 5) !Provide some provenance.
CHARACTER*6 HEAD(N) !A perfect size?
INTEGER X(N) !Integers suffice.
INTEGER LINPR,IN !I/O unit numbers.
LINPR = 6 !Standard output via this unit number.
IN = 10 !Some unit number for the input file.
OPEN (IN,FILE="CSVtest.csv",STATUS="OLD",ACTION="READ") !For formatted input.
READ (IN,*) HEAD !The first line has texts as column headings.
WRITE (LINPR,1) (TRIM(HEAD(I)), I = 1,N),"Sum" !Append a "Sum" column.
1 FORMAT (666(A:",")) !The : sez "stop if no list element awaits".
2 READ (IN,*,END = 10) X !Read a line's worth of numbers, separated by commas or spaces.
WRITE (LINPR,3) X,SUM(X) !Write, with a total appended.
3 FORMAT (666(I0:",")) !I0 editing uses only as many columns as are needed.
GO TO 2 !Do it again.
10 CLOSE (IN) !All done.
END !That's all.

View file

@ -0,0 +1,20 @@
' FB 1.05.0 Win64
Open "manip.csv" For Input As #1 ' existing CSV file
Open "manip2.csv" For Output As #2 ' new CSV file for writing changed data
Dim header As String
Line Input #1, header
header += ",SUM"
Print #2, header
Dim As Integer c1, c2, c3, c4, c5, sum
While Not Eof(1)
Input #1, c1, c2, c3, c4, c5
sum = c1 + c2 + c3 + c4 + c5
Write #2, c1, c2, c3, c4, c5, sum
Wend
Close #1
Close #2

View file

@ -0,0 +1,45 @@
import io.{lines, PrintWriter}
data Table( header, rows )
def read( file ) =
l = lines( file )
def next = vector( l.next().split(',') )
if l.isEmpty() then
return Table( vector(), [] )
header = next()
rows = seq()
while l.hasNext()
rows += next()
Table( header, rows.toList() )
def write( table, out ) =
w = if out is String then PrintWriter( out ) else out
w.println( table.header.mkString(',') )
for r <- table.rows
w.println( r.mkString(',') )
if out is String
w.close()
def updateRow( header, row, updates ) =
r = dict( (header(i), row(i)) | i <- 0:header.length() )
updates( r )
vector( r(f) | f <- header )
def update( table, updates ) =
Table( table.header, (updateRow(table.header, r, updates) | r <- table.rows).toList() )
def addColumn( table, column, updates ) =
Table( table.header + [column], (updateRow(table.header + [column], r + [null], updates) | r <- table.rows).toList() )
t = addColumn( read('test.csv'), 'SUM', r -> r('SUM') = sum(int(v) | (_, v) <- r if v != null) )
write( t, 'test_out.csv' )
write( t, System.out )

View file

@ -0,0 +1,42 @@
include "NSLog.incl"
include resources "rosetta_csv.csv"
/*
This ASCII text data is saved as a resource file
named "rosetta_csv.csv" in the application bundle.
C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20
*/
void local fn ManipulateCSV
CFURLRef url = fn BundleURLForResource( fn BundleMain, @"rosetta_csv", @"csv", NULL )
CFStringRef csvString = fn StringWithContentsOfURL( url, NSUTF8StringEncoding, NULL )
CFArrayRef csvArray = fn StringComponentsSeparatedByCharactersInSet( csvString, fn CharacterSetNewlineSet )
CFMutableStringRef mutStr = fn MutableStringWithCapacity(0)
long i
MutableStringAppendFormat( mutStr, @",%@,SUM,AVG\n", csvArray[0] )
for i = 1 to len(csvArray) - 1
CFArrayRef nums = fn StringComponentsSeparatedByString( csvArray[i], @"," )
CFNumberRef sum = fn ObjectValueForKeyPath( nums, @"@sum.self" )
CFNumberRef avg = fn ObjectValueForKeyPath( nums, @"@avg.self" )
MutableStringAppendFormat( mutStr, @"R%ld,%@,%@,%@\n",i,csvArray[i],sum,avg )
next
NSLog( @"%@", mutStr )
CFURLRef desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
url = fn URLByAppendingPathComponent( desktopURL, @"final_csv.csv" )
fn StringWriteToURL( mutStr, url, YES, NSUTF8StringEncoding, NULL )
end fn
fn ManipulateCSV
HandleEvents

View file

@ -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

View file

@ -0,0 +1,58 @@
package main
import (
"encoding/csv"
"log"
"os"
"strconv"
)
func main() {
rows := readSample()
appendSum(rows)
writeChanges(rows)
}
func readSample() [][]string {
f, err := os.Open("sample.csv")
if err != nil {
log.Fatal(err)
}
rows, err := csv.NewReader(f).ReadAll()
f.Close()
if err != nil {
log.Fatal(err)
}
return rows
}
func appendSum(rows [][]string) {
rows[0] = append(rows[0], "SUM")
for i := 1; i < len(rows); i++ {
rows[i] = append(rows[i], sum(rows[i]))
}
}
func sum(row []string) string {
sum := 0
for _, s := range row {
x, err := strconv.Atoi(s)
if err != nil {
return "NA"
}
sum += x
}
return strconv.Itoa(sum)
}
func writeChanges(rows [][]string) {
f, err := os.Create("output.csv")
if err != nil {
log.Fatal(err)
}
err = csv.NewWriter(f).WriteAll(rows)
f.Close()
if err != nil {
log.Fatal(err)
}
}

View file

@ -0,0 +1,8 @@
def csv = []
def loadCsv = { source -> source.splitEachLine(/,/) { csv << it.collect { it } } }
def saveCsv = { target -> target.withWriter { writer -> csv.each { writer.println it.join(',') } } }
loadCsv new File('csv.txt')
csv[0][0] = 'Column0'
(1..4).each { i -> csv[i][i] = i * 100 }
saveCsv new File('csv_out.txt')

View file

@ -0,0 +1,45 @@
import Data.Array (Array(..), (//), bounds, elems, listArray)
import Data.List (intercalate)
import Control.Monad (when)
import Data.Maybe (isJust)
delimiters :: String
delimiters = ",;:"
fields :: String -> [String]
fields [] = []
fields xs =
let (item, rest) = break (`elem` delimiters) xs
(_, next) = break (`notElem` delimiters) rest
in item : fields next
unfields :: Maybe (Array (Int, Int) String) -> [String]
unfields Nothing = []
unfields (Just a) = every fieldNumber $ elems a
where
((_, _), (_, fieldNumber)) = bounds a
every _ [] = []
every n xs =
let (y, z) = splitAt n xs
in intercalate "," y : every n z
fieldArray :: [[String]] -> Maybe (Array (Int, Int) String)
fieldArray [] = Nothing
fieldArray xs =
Just $ listArray ((1, 1), (length xs, length $ head xs)) $ concat xs
fieldsFromFile :: FilePath -> IO (Maybe (Array (Int, Int) String))
fieldsFromFile = fmap (fieldArray . map fields . lines) . readFile
fieldsToFile :: FilePath -> Maybe (Array (Int, Int) String) -> IO ()
fieldsToFile f = writeFile f . unlines . unfields
someChanges :: Maybe (Array (Int, Int) String)
-> Maybe (Array (Int, Int) String)
someChanges =
fmap (// [((1, 1), "changed"), ((3, 4), "altered"), ((5, 2), "modified")])
main :: IO ()
main = do
a <- fieldsFromFile "example.txt"
when (isJust a) $ fieldsToFile "output.txt" $ someChanges a

View file

@ -0,0 +1,49 @@
{-# LANGUAGE FlexibleContexts,
TypeFamilies,
NoMonomorphismRestriction #-}
import Data.List (intercalate)
import Data.List.Split (splitOn)
import Lens.Micro
(<$$>) :: (Functor f1, Functor f2) =>
(a -> b) -> f1 (f2 a) -> f1 (f2 b)
(<$$>) = fmap . fmap
------------------------------------------------------------
-- reading and writing
newtype CSV = CSV { values :: [[String]] }
readCSV :: String -> CSV
readCSV = CSV . (splitOn "," <$$> lines)
instance Show CSV where
show = unlines . map (intercalate ",") . values
------------------------------------------------------------
-- construction and combination
mkColumn, mkRow :: [String] -> CSV
(<||>), (<==>) :: CSV -> CSV -> CSV
mkColumn lst = CSV $ sequence [lst]
mkRow lst = CSV [lst]
CSV t1 <||> CSV t2 = CSV $ zipWith (++) t1 t2
CSV t1 <==> CSV t2 = CSV $ t1 ++ t2
------------------------------------------------------------
-- access and modification via lenses
table = lens values (\csv t -> csv {values = t})
row i = table . ix i . traverse
col i = table . traverse . ix i
item i j = table . ix i . ix j
------------------------------------------------------------
sample = readCSV "C1, C2, C3, C4, C5\n\
\1, 5, 9, 13, 17\n\
\2, 6, 10, 14, 18\n\
\3, 7, 11, 15, 19\n\
\4, 8, 12, 16, 20"

View file

@ -0,0 +1,2 @@
sampleSum = sample <||> (mkRow ["SUM"] <==> mkColumn sums)
where sums = map (show . sum) (read <$$> drop 1 (values sample))

View file

@ -0,0 +1,11 @@
import Utils # To get CSV procedures
procedure main(A)
f := open(A[1]) | &input
i := 1
write(!f) # header line(?)
every csv := parseCSV(!f) do {
csv[i+:=1] *:= 100
write(encodeCSV(csv))
}
end

View file

@ -0,0 +1,3 @@
data=: (','&splitstring);.2 freads 'rc_csv.csv' NB. read and parse data
data=: (<'"spam"') (<2 3)} data NB. amend cell in 3rd row, 4th column (0-indexing)
'rc_outcsv.csv' fwrites~ ;<@(','&joinstring"1) data NB. format and write out amended data

View file

@ -0,0 +1,4 @@
require 'tables/csv'
data=: makenum readcsv 'rc_csv.csv' NB. read data and convert cells to numeric where possible
data=: (<'spam') (2 3;3 0)} data NB. amend 2 cells
data writecsv 'rc_outcsv.csv' NB. write out amended data. Strings are double-quoted

View file

@ -0,0 +1,5 @@
require 'tables/csv'
'hdr data'=: split readcsv 'rc_csv.csv' NB. read data, split the header & data
hdr=: hdr , <'SUM' NB. add title for extra column to header
data=: <"0 (,. +/"1) makenum data NB. convert to numeric, sum rows & append column
(hdr,data) writecsv 'rc_out.csv'

View file

@ -0,0 +1,2 @@
sumCSVrows=: writecsv~ (((<'SUM') ,~ {.) , [: (<"0)@(,. +/"1) makenum@}.)@readcsv
'rc_out.csv' sumCSVrows 'rc.csv'

View file

@ -0,0 +1,7 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

View file

@ -0,0 +1,68 @@
public class CSV {
public static void main(String[] args) throws IOException {
CSV csv = new CSV("data.csv");
csv.sumAllRows();
csv.write();
}
private final File file;
private List<List<String>> data;
public CSV(File file) throws IOException {
this.file = file;
open();
}
/* convenience constructor */
public CSV(String path) throws IOException {
this(new File(path));
}
public void sumAllRows() {
appendColumn("SUM");
int sum;
int length;
for (int index = 1; index < data.size(); index++) {
sum = sum(data.get(index));
length = data.get(index).size();
data.get(index).set(length - 1, String.valueOf(sum));
}
}
private int sum(List<String> row) {
int sum = 0;
for (int index = 0; index < row.size() - 1; index++)
sum += Integer.parseInt(row.get(index));
return sum;
}
private void appendColumn(String title) {
List<String> titles = data.get(0);
titles.add(title);
/* append an empty cell to each row */
for (int index = 1; index < data.size(); index++)
data.get(index).add("");
}
private void open() throws IOException {
try (BufferedReader reader = new BufferedReader(new FileReader(file))) {
data = new ArrayList<>();
String line;
while ((line = reader.readLine()) != null) {
/* using a limit of -1 will preserve trailing commas */
data.add(new ArrayList<>(List.of(line.split(",", -1))));
}
}
}
public void write() throws IOException {
try (FileWriter writer = new FileWriter(file)) {
String newline = System.lineSeparator();
for (List<String> row : data) {
writer.write(String.join(",", row));
writer.write(newline);
}
writer.flush();
}
}
}

View file

@ -0,0 +1,104 @@
import java.io.*;
import java.awt.Point;
import java.util.HashMap;
import java.util.Scanner;
public class CSV {
private HashMap<Point, String> _map = new HashMap<Point, String>();
private int _cols;
private int _rows;
public void open(File file) throws FileNotFoundException, IOException {
open(file, ',');
}
public void open(File file, char delimiter)
throws FileNotFoundException, IOException {
Scanner scanner = new Scanner(file);
scanner.useDelimiter(Character.toString(delimiter));
clear();
while(scanner.hasNextLine()) {
String[] values = scanner.nextLine().split(Character.toString(delimiter));
int col = 0;
for ( String value: values ) {
_map.put(new Point(col, _rows), value);
_cols = Math.max(_cols, ++col);
}
_rows++;
}
scanner.close();
}
public void save(File file) throws IOException {
save(file, ',');
}
public void save(File file, char delimiter) throws IOException {
FileWriter fw = new FileWriter(file);
BufferedWriter bw = new BufferedWriter(fw);
for (int row = 0; row < _rows; row++) {
for (int col = 0; col < _cols; col++) {
Point key = new Point(col, row);
if (_map.containsKey(key)) {
bw.write(_map.get(key));
}
if ((col + 1) < _cols) {
bw.write(delimiter);
}
}
bw.newLine();
}
bw.flush();
bw.close();
}
public String get(int col, int row) {
String val = "";
Point key = new Point(col, row);
if (_map.containsKey(key)) {
val = _map.get(key);
}
return val;
}
public void put(int col, int row, String value) {
_map.put(new Point(col, row), value);
_cols = Math.max(_cols, col+1);
_rows = Math.max(_rows, row+1);
}
public void clear() {
_map.clear();
_cols = 0;
_rows = 0;
}
public int rows() {
return _rows;
}
public int cols() {
return _cols;
}
public static void main(String[] args) {
try {
CSV csv = new CSV();
csv.open(new File("test_in.csv"));
csv.put(0, 0, "Column0");
csv.put(1, 1, "100");
csv.put(2, 2, "200");
csv.put(3, 3, "300");
csv.put(4, 4, "400");
csv.save(new File("test_out.csv"));
} catch (Exception e) {
}
}
}

View file

@ -0,0 +1,125 @@
import java.io.*;
import java.util.*;
import org.apache.commons.csv.*;
public class RCsv {
private static final String NL = System.getProperty("line.separator");
private static final String FILENAME_IR = "data/csvtest_in.csv";
private static final String FILENAME_OR = "data/csvtest_sum.csv";
private static final String COL_NAME_SUM = "SUM, \"integers\""; // demonstrate white space, comma & quote handling
public static void main(String[] args) {
Reader iCvs = null;
Writer oCvs = null;
System.out.println(textFileContentsToString(FILENAME_IR));
try {
iCvs = new BufferedReader(new FileReader(FILENAME_IR));
oCvs = new BufferedWriter(new FileWriter(FILENAME_OR));
processCsv(iCvs, oCvs);
}
catch (IOException ex) {
ex.printStackTrace();
}
finally {
try {
if (iCvs != null) { iCvs.close(); }
if (oCvs != null) { oCvs.close(); }
}
catch (IOException ex) {
ex.printStackTrace();
}
}
System.out.println(textFileContentsToString(FILENAME_OR));
return;
}
public static void processCsv(Reader iCvs, Writer oCvs) throws IOException {
CSVPrinter printer = null;
try {
printer = new CSVPrinter(oCvs, CSVFormat.DEFAULT.withRecordSeparator(NL));
List<String> oCvsHeaders;
List<String> oCvsRecord;
CSVParser records = CSVFormat.DEFAULT.withHeader().parse(iCvs);
Map<String, Integer> irHeader = records.getHeaderMap();
oCvsHeaders = new ArrayList<String>(Arrays.asList((irHeader.keySet()).toArray(new String[0])));
oCvsHeaders.add(COL_NAME_SUM);
printer.printRecord(oCvsHeaders);
for (CSVRecord record : records) {
oCvsRecord = record2list(record, oCvsHeaders);
printer.printRecord(oCvsRecord);
}
}
finally {
if (printer != null) {
printer.close();
}
}
return;
}
private static List<String> record2list(CSVRecord record, List<String> oCvsHeaders) {
List<String> cvsRecord;
Map<String, String> rMap = record.toMap();
long recNo = record.getRecordNumber();
rMap = alterRecord(rMap, recNo);
int sum = 0;
sum = summation(rMap);
rMap.put(COL_NAME_SUM, String.valueOf(sum));
cvsRecord = new ArrayList<String>();
for (String key : oCvsHeaders) {
cvsRecord.add(rMap.get(key));
}
return cvsRecord;
}
private static Map<String, String> alterRecord(Map<String, String> rMap, long recNo) {
int rv;
Random rg = new Random(recNo);
rv = rg.nextInt(50);
String[] ks = rMap.keySet().toArray(new String[0]);
int ix = rg.nextInt(ks.length);
long yv = 0;
String ky = ks[ix];
String xv = rMap.get(ky);
if (xv != null && xv.length() > 0) {
yv = Long.valueOf(xv) + rv;
rMap.put(ks[ix], String.valueOf(yv));
}
return rMap;
}
private static int summation(Map<String, String> rMap) {
int sum = 0;
for (String col : rMap.keySet()) {
String nv = rMap.get(col);
sum += nv != null && nv.length() > 0 ? Integer.valueOf(nv) : 0;
}
return sum;
}
private static String textFileContentsToString(String filename) {
StringBuilder lineOut = new StringBuilder();
Scanner fs = null;
try {
fs = new Scanner(new File(filename));
lineOut.append(filename);
lineOut.append(NL);
while (fs.hasNextLine()) {
String line = fs.nextLine();
lineOut.append(line);
lineOut.append(NL);
}
}
catch (FileNotFoundException ex) {
// TODO Auto-generated catch block
ex.printStackTrace();
}
finally {
if (fs != null) {
fs.close();
}
}
return lineOut.toString();
}
}

View file

@ -0,0 +1,31 @@
public static void main(String[] args) throws IOException {
// 1st, config the CSV reader with line separator
CsvParserSettings settings = new CsvParserSettings();
settings.getFormat().setLineSeparator("\n");
// 2nd, config the CSV reader with row processor attaching the bean definition
BeanListProcessor<Employee> rowProcessor = new BeanListProcessor<Employee>(Employee.class);
settings.setRowProcessor(rowProcessor);
// 3rd, creates a CSV parser with the configs
CsvParser parser = new CsvParser(settings);
// 4th, parse all rows from the CSF file into the list of beans you defined
parser.parse(new FileReader("/examples/employees.csv"));
List<Employee> resolvedBeans = rowProcessor.getBeans();
// 5th, Store, Delete duplicates, Re-arrange the words in specific order
// ......
// 6th, Write the listed of processed employee beans out to a CSV file.
CsvWriterSettings writerSettings = new CsvWriterSettings();
// 6.1 Creates a BeanWriterProcessor that handles annotated fields in the Employee class.
writerSettings.setRowWriterProcessor(new BeanWriterProcessor<Employee>(Employee.class));
// 6.2 persistent the employee beans to a CSV file.
CsvWriter writer = new CsvWriter(new FileWriter("/examples/processed_employees.csv"), writerSettings);
writer.processRecords(resolvedBeans);
writer.writeRows(new ArrayList<List<Object>>());
}

View file

@ -0,0 +1,76 @@
(function () {
'use strict';
// splitRegex :: Regex -> String -> [String]
function splitRegex(rgx, s) {
return s.split(rgx);
}
// lines :: String -> [String]
function lines(s) {
return s.split(/[\r\n]/);
}
// unlines :: [String] -> String
function unlines(xs) {
return xs.join('\n');
}
// macOS JavaScript for Automation version of readFile.
// Other JS contexts will need a different definition of this function,
// and some may have no access to the local file system at all.
// readFile :: FilePath -> maybe String
function readFile(strPath) {
var error = $(),
str = ObjC.unwrap(
$.NSString.stringWithContentsOfFileEncodingError(
$(strPath)
.stringByStandardizingPath,
$.NSUTF8StringEncoding,
error
)
);
return error.code ? error.localizedDescription : str;
}
// macOS JavaScript for Automation version of writeFile.
// Other JS contexts will need a different definition of this function,
// and some may have no access to the local file system at all.
// writeFile :: FilePath -> String -> IO ()
function writeFile(strPath, strText) {
$.NSString.alloc.initWithUTF8String(strText)
.writeToFileAtomicallyEncodingError(
$(strPath)
.stringByStandardizingPath, false,
$.NSUTF8StringEncoding, null
);
}
// EXAMPLE - appending a SUM column
var delimCSV = /,\s*/g;
var strSummed = unlines(
lines(readFile('~/csvSample.txt'))
.map(function (x, i) {
var xs = x ? splitRegex(delimCSV, x) : [];
return (xs.length ? xs.concat(
// 'SUM' appended to first line, others summed.
i > 0 ? xs.reduce(
function (a, b) {
return a + parseInt(b, 10);
}, 0
).toString() : 'SUM'
) : []).join(',');
})
);
return (
writeFile('~/csvSampleSummed.txt', strSummed),
strSummed
);
})();

View file

@ -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;
}
});

View file

@ -0,0 +1,20 @@
# Input: a single row
# Omit empty rows
def read_csv:
if length>0 then split(",") else empty end ;
# Input: an array
# Output: the same array but with an additional summation column.
# If .[0] is a number, then it is assumed the entire row consists of numbers or numeric strings;
# otherwise, 0 is added
def add_sum:
(if .[0] | type == "number" then (map(tonumber) | add) else 0 end) as $sum
| . + [$sum] ;
# `tocsv` is only needed if fields should only be quoted by necessity:
def tocsv:
map( if type == "string" and test("[,\"\r\n]") then "\"\(.)\"" else . end )
| join(",");
( input | read_csv | . + ["SUM"] | @csv),
(inputs | read_csv | add_sum | @csv)

View file

@ -0,0 +1,8 @@
using DataFrames, CSV
ifn = "csv_data_manipulation_in.dat"
ofn = "csv_data_manipulation_out.dat"
df = CSV.read(ifn, DataFrame)
df.SUM = sum.(eachrow(df))
CSV.write(ofn, df)

View file

@ -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
}

View file

@ -0,0 +1,99 @@
----------------------------------------
-- Simplified CSV parser (without escape character support etc.).
-- First line is interrepted as header with column names.
-- @param {string} csvStr
-- @param {string} [sep=","] - single char as string
-- @param {string} [eol=RETURN]
-- @return {propList}
----------------------------------------
on parseSimpleCSVString (csvStr, sep, eol)
if voidP(sep) then sep=","
if voidP(eol) then eol = RETURN
lines = explode(eol, csvStr)
if lines.getLast()="" then lines.deleteAt(lines.count)
res = [:]
res[#header] = explode(sep, lines[1])
res[#data] = []
cnt = lines.count
repeat with i = 2 to cnt
res[#data].append(explodeBySingleChar(sep, lines[i]))
end repeat
return res
end
----------------------------------------
-- Simplified CSV creater (without escape character support etc.).
-- @param {propList} csvData
-- @param {string} [sep=","]
-- @param {string} [eol=RETURN]
-- @return {string}
----------------------------------------
on createSimpleCSVString (csvData, sep, eol)
if voidP(sep) then sep=","
if voidP(eol) then eol = RETURN
res = ""
put implode(sep, csvData[#header])&eol after res
cnt = csvData[#data].count
repeat with i = 1 to cnt
put implode(sep, csvData[#data][i])&eol after res
end repeat
return res
end
----------------------------------------
-- Explodes string into list
-- @param {string} delim
-- @param {string} str
-- @return {list}
----------------------------------------
on explode (delim, str)
if delim.length=1 then return explodeBySingleChar(delim, str)
l = []
if voidP(str) then return l
dl = delim.length
repeat while true
pos = offset(delim, str)
if pos=0 then exit repeat
l.add(str.char[1..pos-1])
delete char 1 to pos+dl-1 of str
end repeat
if pos=0 then pos = 1-dl
l.add(str.char[pos+dl..str.length])
return l
end
----------------------------------------
-- Explode string into list based on single char delimiter
-- (uses Lingo's build-in 'item' support, therefor faster)
-- @param {string} delim
-- @param {string} str
-- @return {list}
----------------------------------------
on explodeBySingleChar (delim, str)
l = []
if voidP(str) then return l
od = _player.itemDelimiter
_player.itemDelimiter = delim
cnt = str.item.count
repeat with i = 1 to cnt
l.add(str.item[i])
end repeat
_player.itemDelimiter = od
return l
end
----------------------------------------
-- Implodes list into string
-- @param {string} delim
-- @param {list} l
-- @return {string}
----------------------------------------
on implode (delim, l)
str = ""
cnt = l.count
repeat with i = 1 to cnt
put l[i]&delim after str
end repeat
delete char (str.length-delim.length+1) to str.length of str
return str
end

View file

@ -0,0 +1,44 @@
sep = ","
eol = numtochar(10)
-- load CSV string from file
fn = _movie.path & "file.csv"
fp = xtra("fileIO").new()
fp.openFile(fn, 1)
csvStr = fp.readFile()
fp.closeFile()
-- parse CSV string into propList
csvData = parseSimpleCSVString(csvStr, sep, eol)
-- add SUM column
csvData[#header].append("SUM")
repeat with row in csvData[#data]
sum = 0
repeat with cell in row
sum = sum+integer(cell)
end repeat
row.append(sum)
end repeat
-- create CSV string from updated propList
csvString = createSimpleCSVString(csvData, sep, eol)
-- save CSV string to file
fn = _movie.path & "file.csv"
fp.openFile(fn, 2)
if not fp.status() then fp.delete()
fp.createFile(fn)
fp.openFile(fn, 2)
fp.writeString(csvString)
fp.closeFile()
-- show the CSV string
put csvString
-- "C1,C2,C3,C4,C5,SUM
1,5,9,13,17,45
2,6,10,14,18,50
3,7,11,15,19,55
4,8,12,16,20,60
"

View file

@ -0,0 +1,31 @@
local csv={}
for line in io.lines('file.csv') do
table.insert(csv, {})
local i=1
for j=1,#line do
if line:sub(j,j) == ',' then
table.insert(csv[#csv], line:sub(i,j-1))
i=j+1
end
end
table.insert(csv[#csv], line:sub(i,j))
end
table.insert(csv[1], 'SUM')
for i=2,#csv do
local sum=0
for j=1,#csv[i] do
sum=sum + tonumber(csv[i][j])
end
if sum>0 then
table.insert(csv[i], sum)
end
end
local newFileData = ''
for i=1,#csv do
newFileData=newFileData .. table.concat(csv[i], ',') .. '\n'
end
local file=io.open('file.csv', 'w')
file:write(newFileData)

View file

@ -0,0 +1,47 @@
Module Checkit {
Function Sum {
Long c=0
while not empty {
c+=number
}
=c
}
Document CSV$ = {C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20
}
\\ export encoded to UTF-16LE
Save.Doc CSV$, "data1.csv", 0
\\ Open Wide read UTF-16LE
\\ use standard colum sep. as ","
\\ use standard decimal point char
\\ use standard (non json style string)
\\ True = use bare strings (without "")
Input With "",,,true
Write With"",,,true
\\ for excel csv use Input With chr$(9),,true, true
Open "data1.csv" for Wide Input as #M
Open "data2.csv" for Wide Output as #M1
Input #M, h1$, h2$, h3$, h4$, h5$
Write #M1, h1$, h2$, h3$, h4$, h5$
Print h1$, h2$, h3$, h4$, h5$
While not Eof(#M) {
Input #M, A1, A2, A3, A4, A5
Write #M1, A1, A2, A3, A4, A5, Sum(A1, A2, A3, A4, A5)
Print A1, A2, A3, A4, A5
}
close #M1
Close #M
Open "data2.csv" for Wide Input as #M
Input #M, h1$, h2$, h3$, h4$, h5$
Print h1$, h2$, h3$, h4$, h5$
While not Eof(#M) {
Input #M, A1, A2, A3, A4, A5, Sum
Print A1, A2, A3, A4, A5, Sum
}
Close #M
}
Checkit

View file

@ -0,0 +1,13 @@
filename='data.csv';
fid = fopen(filename);
header = fgetl(fid);
fclose(fid);
X = dlmread(filename,',',1,0);
fid = fopen('data.out.csv','w+');
fprintf(fid,'%s,sum\n',header);
for k=1:size(X,1),
fprintf(fid,"%i,",X(k,:));
fprintf(fid,"%i\n",sum(X(k,:)));
end;
fclose(fid);

View file

@ -0,0 +1,4 @@
filename='data.csv';
data = readtable(filename);
data.SUM = sum([data{:,:}],2);
writetable(data,filename);

View file

@ -0,0 +1,3 @@
M := ImportMatrix("data.csv",source=csv);
M(..,6) := < "Total", seq( add(M[i,j], j=1..5), i=2..5 ) >;
ExportMatrix("data_out.csv",M,target=csv);

View file

@ -0,0 +1,24 @@
> M := ImportMatrix("data.csv",source=csv);
["C1" "C2" "C3" "C4" "C5"]
[ ]
[ 1 5 9 13 17 ]
[ ]
M := [ 2 6 10 14 18 ]
[ ]
[ 3 7 11 15 19 ]
[ ]
[ 4 8 12 16 20 ]
> M(..,6) := < "Total", seq( add(M[i,j], j=1..5), i=2..5 ) >;
["C1" "C2" "C3" "C4" "C5" "Total"]
[ ]
[ 1 5 9 13 17 45 ]
[ ]
M := [ 2 6 10 14 18 50 ]
[ ]
[ 3 7 11 15 19 55 ]
[ ]
[ 4 8 12 16 20 60 ]
> ExportMatrix("data_out.csv",M,target=csv);
96

View file

@ -0,0 +1,6 @@
iCSV=Import["test.csv"]
->{{"C1","C2","C3","C4","C5"},{1,5,9,13,17},{2,6,10,14,18},{3,7,11,15,19},{4,8,12,16,20}}
iCSV = Transpose@
Append[Transpose[iCSV], Join[{"Sum"}, Total /@ Drop[iCSV, 1]]];
iCSV // MatrixForm
Export["test.csv",iCSV];

View file

@ -0,0 +1,18 @@
def sum(record)
sum = 0
for i in range(1, len(record) - 1)
sum = sum + int(record ~ i)
end for
return sum
end def
open "file.csv"
add "SUM"
for i in range($dbsize, 1)
(i ~ @"SUM") = sum(#i)
end for
write

View file

@ -0,0 +1,122 @@
/* NetRexx */
options replace format comments java crossref symbols
import org.apache.commons.csv.
-- =============================================================================
class RCsv public final
properties private constant
NL = String System.getProperty("line.separator")
COL_NAME_SUM = String 'SUM, "integers"'
CSV_IFILE = 'data/csvtest_in.csv'
CSV_OFILE = 'data/csvtest_sumRexx.csv'
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method main(args = String[]) public static
Arg = Rexx(args)
iCvs = Reader null
oCvs = Writer null
parse arg ifile ofile .
if ifile = '', ifile = '.' then ifile = CSV_IFILE
if ofile = '', ofile = '.' then ofile = CSV_OFILE
say textFileContentsToString(ifile)
do
iCvs = BufferedReader(FileReader(ifile))
oCvs = BufferedWriter(FileWriter(ofile))
processCsv(iCvs, oCvs);
catch ex = IOException
ex.printStackTrace();
finally
do
if iCvs \= null then iCvs.close()
if oCvs \= null then oCvs.close()
catch ex = IOException
ex.printStackTrace()
end
end
say textFileContentsToString(ofile)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method processCsv(iCvs = Reader, oCvs = Writer) public static binary signals IOException
printer = CSVPrinter null
do
printer = CSVPrinter(oCvs, CSVFormat.DEFAULT.withRecordSeparator(NL))
oCvsHeaders = java.util.List
oCvsRecord = java.util.List
records = CSVFormat.DEFAULT.withHeader(String[0]).parse(iCvs)
irHeader = records.getHeaderMap()
oCvsHeaders = ArrayList(Arrays.asList((irHeader.keySet()).toArray(String[0])))
oCvsHeaders.add(COL_NAME_SUM)
printer.printRecord(oCvsHeaders)
recordIterator = records.iterator()
record = CSVRecord
loop while recordIterator.hasNext()
record = CSVRecord recordIterator.next()
oCvsRecord = record2list(record, oCvsHeaders)
printer.printRecord(oCvsRecord)
end
finally
if printer \= null then printer.close()
end
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method record2list(record = CSVRecord, oCvsHeaders = java.util.List) private static binary returns java.util.List
cvsRecord = java.util.List
rMap = record.toMap()
recNo = record.getRecordNumber()
rMap = alterRecord(rMap, recNo)
sum = summation(record.iterator())
rMap.put(COL_NAME_SUM, sum)
cvsRecord = ArrayList()
loop ci = 0 to oCvsHeaders.size() - 1
key = oCvsHeaders.get(ci)
cvsRecord.add(rMap.get(key))
end ci
return cvsRecord
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method alterRecord(rMap = Map, recNo = long) private static binary returns Map
rv = int
rg = Random(recNo)
rv = rg.nextInt(50)
ks = rMap.keySet().toArray(String[0])
ix = rg.nextInt(ks.length)
yv = long 0
ky = ks[ix];
xv = String rMap.get(ky)
if xv \= null & xv.length() > 0 then do
yv = Long.valueOf(xv).longValue() + rv
rMap.put(ks[ix], String.valueOf(yv))
end
return rMap
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method summation(iColumn = Iterator) private static
sum = 0
loop while iColumn.hasNext()
nv = Rexx(String iColumn.next())
if nv = null, nv.length() = 0, \nv.datatype('n') then nv = 0
sum = sum + nv
end
return sum
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method textFileContentsToString(filename) private static
lineOut = ''
fs = Scanner null
do
fs = Scanner(File(filename))
lineOut = lineout || filename || NL
loop while fs.hasNextLine()
line = fs.nextLine()
lineOut = lineout || line || NL
end
catch ex = FileNotFoundException
ex.printStackTrace()
finally
if fs \= null then fs.close()
end
return lineOut

View file

@ -0,0 +1,25 @@
import strutils, streams
let
csv = newFileStream("data.csv", fmRead)
outf = newFileStream("data-out.csv", fmWrite)
var lineNumber = 1
while true:
if atEnd(csv):
break
var line = readLine(csv)
if lineNumber == 1:
line.add(",SUM")
else:
var sum = 0
for n in split(line, ","):
sum += parseInt(n)
line.add(",")
line.add($sum)
outf.writeLine(line)
inc lineNumber

View file

@ -0,0 +1,19 @@
let list_add_last this lst =
List.rev (this :: (List.rev lst))
let () =
let csv = Csv.load "data.csv" in
let fields, data =
(List.hd csv,
List.tl csv)
in
let fields =
list_add_last "SUM" fields
in
let sums =
List.map (fun row ->
let tot = List.fold_left (fun tot this -> tot + int_of_string this) 0 row in
list_add_last (string_of_int tot) row
) data
in
Csv.output_all (Csv.to_channel stdout) (fields :: sums)

View file

@ -0,0 +1,32 @@
use System.IO.File;
use Data.CSV;
class CsvData {
function : Main(args : String[]) ~ Nil {
file_out : FileWriter;
leaving {
if(file_out <> Nil) {
file_out->Close();
};
};
if(args->Size() > 0) {
file_name := args[0];
csv := CsvTable->New(FileReader->ReadFile(file_name));
if(csv->IsParsed()) {
csv->AppendColumn("SUM");
for(i := 1; i < csv->Size(); i += 1;) {
row := csv->Get(i);
sum := row->Sum(row->Size() - 1);
row->Set("SUM", sum->ToString());
};
};
output := csv->ToString();
output->PrintLine();
file_out := FileWriter->New("new-csv.csv");
file_out->WriteString(output);
};
}
}

View file

@ -0,0 +1,18 @@
\\ CSV data manipulation
\\ 10/24/16 aev
\\ processCsv(fn): Where fn is an input path and file name (but no actual extension).
processCsv(fn)=
{my(F, ifn=Str(fn,".csv"), ofn=Str(fn,"r.csv"), cn=",SUM",nf,nc,Vr,svr);
if(fn=="", return(-1));
F=readstr(ifn); nf=#F;
F[1] = Str(F[1],cn);
for(i=2, nf,
Vr=stok(F[i],","); if(i==2,nc=#Vr);
svr=sum(k=1,nc,eval(Vr[k]));
F[i] = Str(F[i],",",svr);
);\\fend i
for(j=1, nf, write(ofn,F[j]))
}
\\ Testing:
processCsv("c:\\pariData\\test");

View file

@ -0,0 +1,36 @@
<?php
// fputcsv() requires at least PHP 5.1.0
// file "data_in.csv" holds input data
// the result is saved in "data_out.csv"
// this version has no error-checking
$handle = fopen('data_in.csv','r');
$handle_output = fopen('data_out.csv','w');
$row = 0;
$arr = array();
while ($line = fgetcsv($handle))
{
$arr[] = $line;
}
//change some data to zeroes
$arr[1][0] = 0; // 1,5,9,13,17 => 0,5,9,13,17
$arr[2][1] = 0; // 2,6,10,14,18 => 2,0,10,14,18
//add sum and write file
foreach ($arr as $line)
{
if ($row==0)
{
array_push($line,"SUM");
}
else
{
array_push($line,array_sum($line));
}
fputcsv($handle_output, $line);
$row++;
}
?>

View file

@ -0,0 +1,65 @@
*process source xref attributes or(!);
csv: Proc Options(Main);
/*********************************************************************
* 19.10.2013 Walter Pachl
* 'erase d:\csv.out'
* 'set dd:in=d:\csv.in,recsize(300)'
* 'set dd:out=d:\csv.out,recsize(300)'
* Say 'Input:'
* 'type csv.in'
* 'csv'
* Say ' '
* Say 'Output:'
* 'type csv.out'
*********************************************************************/
Dcl in Record Input;
Dcl out Record Output;
On Endfile(in) Goto part2;
Dcl (INDEX,LEFT,SUBSTR,TRIM) Builtin;
Dcl (i,j,p,m,n) Bin Fixed(31) Init(0);
Dcl s Char(100) Var;
Dcl iline(10) Char(100) Var;
Dcl a(20,20) Char(10) Var;
Dcl sum Dec Fixed(3);
Dcl oline Char(100) Var;
Do i=1 By 1;
Read File(in) Into(s);
iline(i)=s;
m=i;
Call sep((s));
End;
part2:
Do i=1 To m;
If i=1 Then
oline=iline(1)!!','!!'SUM';
Else Do;
sum=0;
Do j=1 To n;
sum=sum+a(i,j);
End;
oline=iline(i)!!','!!trim(sum);
End;
Write File(out) From(oline);
End;
sep: Procedure(line);
Dcl line Char(*) Var;
loop:
Do j=1 By 1;
p=index(line,',');
If p>0 Then Do;
a(i,j)=left(line,p-1);
line=substr(line,p+1);
End;
Else Do;
a(i,j)=line;
Leave loop;
End;
End;
n=j;
End;
End;

View file

@ -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.

View file

@ -0,0 +1,38 @@
#!/usr/bin/perl
use warnings;
use strict;
use List::Util 'sum';
my @header = split /,/, <>;
# Remove the newline.
chomp $header[-1];
my %column_number;
for my $i (0 .. $#header) {
$column_number{$header[$i]} = $i;
}
my @rows = map [ split /,/ ], <>;
chomp $_->[-1] for @rows;
# Add 1 to the numbers in the 2nd column:
$_->[1]++ for @rows;
# Add C1 into C4:
$_->[ $column_number{C4} ] += $_->[ $column_number{C1} ] for @rows;
# Add sums to both rows and columns.
push @header, 'Sum';
$column_number{Sum} = $#header;
push @$_, sum(@$_) for @rows;
push @rows, [
map {
my $col = $_;
sum(map $_->[ $column_number{$col} ], @rows);
} @header
];
# Print the output.
print join(',' => @header), "\n";
print join(',' => @$_), "\n" for @rows;

View file

@ -0,0 +1,25 @@
#!/usr/bin/perl
use warnings;
use strict;
use Text::CSV;
use List::Util 'sum';
my $csv = 'Text::CSV'->new({eol => "\n"})
or die 'Cannot use CSV: ' . 'Text::CSV'->error_diag;
my $file = shift;
my @rows;
open my $FH, '<', $file or die "Cannot open $file: $!";
my @header = @{ $csv->getline($FH) };
while (my $row = $csv->getline($FH)) {
push @rows, $row;
}
$csv->eof or $csv->error_diag;
#
# The processing is the same.
#
# Print the output.
$csv->print(*STDOUT, $_) for \@header, @rows;

View file

@ -0,0 +1,31 @@
(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">constant</span> <span style="color: #000000;">tcsv</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"""
C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20
"""</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()=</span><span style="color: #004600;">JS</span><span style="color: #0000FF;">?</span><span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #000000;">tcsv</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">):</span><span style="color: #7060A8;">get_text</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"test.csv"</span><span style="color: #0000FF;">,</span><span style="color: #004600;">GT_LF_STRIPPED</span><span style="color: #0000FF;">))</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">split</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">trim</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]),</span><span style="color: #008000;">','</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #008000;">','</span><span style="color: #0000FF;">)&</span><span style="color: #008000;">",SUM"</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">2</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]),</span> <span style="color: #000000;">t</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #008080;">for</span> <span style="color: #000000;">j</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">scanf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">j</span><span style="color: #0000FF;">],</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">)[</span><span style="color: #000000;">1</span><span style="color: #0000FF;">][</span><span style="color: #000000;">1</span><span style="color: #0000FF;">]</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000080;font-style:italic;">-- s[rand(length(s))] = rand(100) -- (if you like)</span>
<span style="color: #000000;">t</span> <span style="color: #0000FF;">&=</span> <span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">lines</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d,%d,%d,%d,%d,%d"</span><span style="color: #0000FF;">,</span><span style="color: #000000;">t</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #000000;">lines</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">,</span><span style="color: #008000;">'\n'</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">platform</span><span style="color: #0000FF;">()!=</span><span style="color: #004600;">JS</span> <span style="color: #008080;">then</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">fn</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">open</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"out.csv"</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"w"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">close</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fn</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">lines</span><span style="color: #0000FF;">)</span>
<!--

View file

@ -0,0 +1,4 @@
(in "data.csv"
(prinl (line) "," "SUM")
(while (split (line) ",")
(prinl (glue "," @) "," (sum format @)) ) )

View file

@ -0,0 +1,33 @@
## Create a CSV file
@"
C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20
"@ -split "`r`n" | Out-File -FilePath .\Temp.csv -Force
## Import each line of the CSV file into an array of PowerShell objects
$records = Import-Csv -Path .\Temp.csv
## Sum the values of the properties of each object
$sums = $records | ForEach-Object {
[int]$sum = 0
foreach ($field in $_.PSObject.Properties.Name)
{
$sum += $_.$field
}
$sum
}
## Add a column (Sum) and its value to each object in the array
$records = for ($i = 0; $i -lt $sums.Count; $i++)
{
$records[$i] | Select-Object *,@{Name='Sum';Expression={$sums[$i]}}
}
## Export the array of modified objects to the CSV file
$records | Export-Csv -Path .\Temp.csv -Force
## Display the object in tabular form
$records | Format-Table -AutoSize

View file

@ -0,0 +1,22 @@
test :- augment('test.csv', 'test.out.csv').
% augment( +InFileName, +OutFileName)
augment(InFile, OutFile) :-
open(OutFile, write, OutStream),
( ( csv_read_file_row(InFile, Row, [line(Line)]),
% Row is of the form row( Item1, Item2, ....).
addrow(Row, Out),
csv_write_stream(OutStream, [Out], []),
fail
)
; close(OutStream)
).
% If the first item in a row is an integer, then append the sum;
% otherwise append 'SUM':
addrow( Term, NewTerm ) :-
Term =.. [F | List],
List = [X|_],
(integer(X) -> sum_list(List, Sum) ; Sum = 'SUM'),
append(List, [Sum], NewList),
NewTerm =.. [F | NewList].

View file

@ -0,0 +1,51 @@
EnableExplicit
#Separator$ = ","
Define fInput$ = "input.csv"; insert path to input file
Define fOutput$ = "output.csv"; insert path to output file
Define header$, row$, field$
Define nbColumns, sum, i
If OpenConsole()
If Not ReadFile(0, fInput$)
PrintN("Error opening input file")
Goto Finish
EndIf
If Not CreateFile(1, fOutput$)
PrintN("Error creating output file")
CloseFile(0)
Goto Finish
EndIf
; Read header row
header$ = ReadString(0)
; Determine number of columns
nbColumns = CountString(header$, ",") + 1
; Change header row
header$ + #Separator$ + "SUM"
; Write to output file
WriteStringN(1, header$)
; Read remaining rows, process and write to output file
While Not Eof(0)
row$ = ReadString(0)
sum = 0
For i = 1 To nbColumns
field$ = StringField(row$, i, #Separator$)
sum + Val(field$)
Next
row$ + #Separator$ + sum
WriteStringN(1, row$)
Wend
CloseFile(0)
CloseFile(1)
Finish:
PrintN("")
PrintN("Press any key to close the console")
Repeat: Delay(10) : Until Inkey() <> ""
CloseConsole()
EndIf

View file

@ -0,0 +1,11 @@
import fileinput
changerow, changecolumn, changevalue = 2, 4, '"Spam"'
with fileinput.input('csv_data_manipulation.csv', inplace=True) as f:
for line in f:
if fileinput.filelineno() == changerow:
fields = line.rstrip().split(',')
fields[changecolumn-1] = changevalue
line = ','.join(fields) + '\n'
print(line, end='')

View file

@ -0,0 +1,22 @@
import csv
from pathlib import Path
from tempfile import NamedTemporaryFile
filepath = Path('data.csv')
temp_file = NamedTemporaryFile('w',
newline='',
delete=False)
with filepath.open() as csv_file, temp_file:
reader = csv.reader(csv_file)
writer = csv.writer(temp_file)
header = next(reader)
writer.writerow(header + ['SUM'])
for row in reader:
row_sum = sum(map(int, row))
writer.writerow(row + [row_sum])
temp_file_path = Path(temp_file.name)
temp_file_path.replace(filepath)

View file

@ -0,0 +1,8 @@
import pandas as pd
filepath = 'data.csv'
df = pd.read_csv(filepath)
rows_sums = df.sum(axis=1)
df['SUM'] = rows_sums
df.to_csv(filepath, index=False)

View file

@ -0,0 +1,3 @@
t:("IIIII";enlist ",")0: `:input.csv / Read CSV file input.csv into table t
t:update SUM:sum value flip t from t / Add SUM column to t
`:output.csv 0: csv 0: t / Write updated table as CSV to output.csv

View file

@ -0,0 +1,9 @@
df <- read.csv(textConnection(
"C1,C2,C3,C4,C5
1,5,9,13,17
2,6,10,14,18
3,7,11,15,19
4,8,12,16,20"))
df$sum <- rowSums(df)
write.csv(df,row.names = FALSE)

View file

@ -0,0 +1 @@
write.csv(df, file = "foo.csv",row.names = FALSE)

View file

@ -0,0 +1,24 @@
/* REXX ***************************************************************
* extend in.csv to add a column containing the sum of the lines' elems
* 21.06.2013 Walter Pachl
**********************************************************************/
csv='in.csv'
Do i=1 By 1 While lines(csv)>0
l=linein(csv)
If i=1 Then
l.i=l',SUM'
Else Do
ol=l
sum=0
Do While l<>''
Parse Var l e ',' l
sum=sum+e
End
l.i=ol','sum
End
End
Call lineout csv
'erase' csv
Do i=1 To i-1
Call lineout csv,l.i
End

View file

@ -0,0 +1,19 @@
/*REXX program reads a CSV file & appends a SUM column (which is the sum of all columns)*/
parse arg iFID . /*obtain optional argument from the CL*/
if iFID=='' | iFID=="," then iFID= 'CSV_SUM.DAT' /*Not specified? Then use the default*/
call linein iFID,1,0 /*position the input file to line one.*/
/* [↑] only needed if pgm is nested. */
do rec=1 while lines(iFID)\==0 /*read the input file (all records). */
x= linein(iFid); y= translate(x, , ',') /*read a rec; change commas to blanks.*/
$= 0 /*initial the sum to zero. */
do j=1 for words(y); _= word(y, j) /*get a CSV value. */
if datatype(_, 'N') then $= $ + _ /*Numeric? Add to $.*/
else $= 'SUM' /*Not? Append "SUM".*/
end /*j*/
@.rec = x','$ /*append the sum to the record. */
end /*rec*/ /*Note: at EOF, REC ≡ # of records+1.*/
say rec-1 ' records read from: ' iFID /* [↓] this elides the need for ERASE*/
call lineout iFID,@.1,1 /*set file ptr to 1st rec., write hdr.*/
do k=2 for rec-2 /*process all the records just read. */
call lineout iFID,@.k /*write the new CSV record (has SUM). */
end /*k*/ /*stick a fork in it, we're all done.*/

View file

@ -0,0 +1,17 @@
#lang racket
(require (planet neil/csv:1:=7) net/url)
(define make-reader
(make-csv-reader-maker
'((separator-chars #\,)
(strip-leading-whitespace? . #t)
(strip-trailing-whitespace? . #t))))
(define (all-rows port)
(define read-row (make-reader port))
(define head (append (read-row) '("SUM")))
(define rows (for/list ([row (in-producer read-row '())])
(define xs (map string->number row))
(append row (list (~a (apply + xs))))))
(define (->string row) (string-join row "," #:after-last "\n"))
(string-append* (map ->string (cons head rows))))

View file

@ -0,0 +1,8 @@
(define csv-file
"C1, C2, C3, C4, C5
1, 5, 9, 13, 17
2, 6, 10, 14, 18
3, 7, 11, 15, 19
4, 8, 12, 16, 20")
(display (all-rows (open-input-string csv-file)))

View file

@ -0,0 +1,10 @@
my $csvfile = './whatever.csv';
my $fh = open($csvfile, :r);
my @header = $fh.get.split(',');
my @csv = map {[.split(',')]>>.Num}, $fh.lines;
close $fh;
my $out = open($csvfile, :w);
$out.say((@header,'SUM').join(','));
$out.say((@$_, [+] @$_).join(',')) for @csv;
close $out;

View file

@ -0,0 +1,5 @@
use Text::CSV;
my $csvfile = './whatever.csv';
my @csv = Text::CSV.parse-file($csvfile);
# modify(@csv); # do whatever;
csv-write-file( @csv, :file($csvfile) );

View file

@ -0,0 +1,4 @@
>>filein: read/lines %file.csv
>>data: copy []
>>foreach item filein [append/only data split item ","]
; [["C1" "C2" "C3" "C4" "C5"] ["1" "5" "9" "13" "17"] ["2" "6" "10" "14" "18"] ["3" "7" "11" "15" "19"]["4" "8" "12" "16" "20"]]

View file

@ -0,0 +1,6 @@
>>forall data [either (index? data) = 1[
append data/1 "SUM"
][
append data/1 to string!
(to integer! data/1/1) + (to integer! data/1/2) + (to integer! data/1/3) + (to integer! data/1/4) + (to integer! data/1/5)
]]

View file

@ -0,0 +1,8 @@
>>foreach item data [append item/6 "^/" repeat c 5 [append item/:c ","]]
>> print data
C1, C2, C3, C4, C5, SUM
1, 5, 9, 13, 17, 45
2, 6, 10, 14, 18, 50
3, 7, 11, 15, 19, 55
4, 8, 12, 16, 20, 60
>>write fileout.csv form data

View file

@ -0,0 +1,34 @@
# Project : CSV data manipulation
load "stdlib.ring"
fnin = "input.csv"
fnout = "output.csv"
fpin = fopen(fnin,"r")
fpout = fopen(fnout,"r")
csv = read(fnin)
nr = 0
csvstr = ""
while not feof(fpin)
sum = 0
nr = nr + 1
line = readline(fpin)
if nr = 1
line = substr(line,nl,"")
line = line + ",SUM"
csvstr = csvstr + line + windowsnl()
else
csvarr = split(line,",")
for n = 1 to len(csvarr)
sum = sum + csvarr[n]
next
line = substr(line,nl,"")
line = line + "," + string(sum)
csvstr = csvstr + line + windowsnl()
ok
end
write(fnout,csvstr)
csvend = read(fnout)
fclose(fpin)
fclose(fpout)
see csvend + nl

Some files were not shown because too many files have changed in this diff Show more