Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
19
Task/CSV-data-manipulation/00DESCRIPTION
Normal file
19
Task/CSV-data-manipulation/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
[[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.
|
||||
|
||||
The task here is to 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>
|
||||
13
Task/CSV-data-manipulation/AWK/csv-data-manipulation.awk
Normal file
13
Task/CSV-data-manipulation/AWK/csv-data-manipulation.awk
Normal 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
|
||||
}
|
||||
|
|
@ -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
|
||||
129
Task/CSV-data-manipulation/C++/csv-data-manipulation.cpp
Normal file
129
Task/CSV-data-manipulation/C++/csv-data-manipulation.cpp
Normal 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;
|
||||
}
|
||||
108
Task/CSV-data-manipulation/C-sharp/csv-data-manipulation.cs
Normal file
108
Task/CSV-data-manipulation/C-sharp/csv-data-manipulation.cs
Normal file
|
|
@ -0,0 +1,108 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.IO;
|
||||
|
||||
namespace CSV
|
||||
{
|
||||
class CSV
|
||||
{
|
||||
private Dictionary<Tuple<int, int>, string> _data;
|
||||
private int _rows;
|
||||
private int _cols;
|
||||
|
||||
public int Rows { get { return _rows; } }
|
||||
public int Cols { get { return _cols; } }
|
||||
|
||||
public CSV()
|
||||
{
|
||||
Clear();
|
||||
}
|
||||
|
||||
public void Clear()
|
||||
{
|
||||
_rows = 0;
|
||||
_cols = 0;
|
||||
_data = new Dictionary<Tuple<int, int>, string>();
|
||||
}
|
||||
|
||||
public void Open(StreamReader stream, char delim = ',')
|
||||
{
|
||||
string line;
|
||||
int col = 0;
|
||||
int row = 0;
|
||||
|
||||
Clear();
|
||||
|
||||
while ((line = stream.ReadLine()) != null)
|
||||
{
|
||||
if (line.Length > 0)
|
||||
{
|
||||
string[] values = line.Split(delim);
|
||||
col = 0;
|
||||
foreach (var value in values)
|
||||
{
|
||||
this[col,row] = value;
|
||||
col++;
|
||||
}
|
||||
row++;
|
||||
}
|
||||
}
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public void Save(StreamWriter stream, char delim = ',')
|
||||
{
|
||||
for (int row = 0; row < _rows; row++)
|
||||
{
|
||||
for (int col = 0; col < _cols; col++)
|
||||
{
|
||||
stream.Write(this[col, row]);
|
||||
if (col < _cols - 1)
|
||||
{
|
||||
stream.Write(delim);
|
||||
}
|
||||
}
|
||||
stream.WriteLine();
|
||||
}
|
||||
stream.Flush();
|
||||
stream.Close();
|
||||
}
|
||||
|
||||
public string this[int col, int row]
|
||||
{
|
||||
get
|
||||
{
|
||||
try
|
||||
{
|
||||
return _data[new Tuple<int, int>(col, row)];
|
||||
}
|
||||
catch
|
||||
{
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_data[new Tuple<int, int>(col, row)] = value.ToString().Trim();
|
||||
_rows = Math.Max(_rows, row + 1);
|
||||
_cols = Math.Max(_cols, col + 1);
|
||||
}
|
||||
}
|
||||
|
||||
static void Main(string[] args)
|
||||
{
|
||||
CSV csv = new CSV();
|
||||
|
||||
csv.Open(new StreamReader(@"test_in.csv"));
|
||||
csv[0, 0] = "Column0";
|
||||
csv[1, 1] = "100";
|
||||
csv[2, 2] = "200";
|
||||
csv[3, 3] = "300";
|
||||
csv[4, 4] = "400";
|
||||
csv.Save(new StreamWriter(@"test_out.csv"));
|
||||
}
|
||||
}
|
||||
}
|
||||
14
Task/CSV-data-manipulation/Clojure/csv-data-manipulation.clj
Normal file
14
Task/CSV-data-manipulation/Clojure/csv-data-manipulation.clj
Normal 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)))))
|
||||
|
|
@ -0,0 +1,35 @@
|
|||
(defun csv_to_nested_list (filename seperator)
|
||||
"Reads the csv to a nested lisp list, where each sublist represents a line.
|
||||
Each line is read in as a string, the commas are substituted by spaces and
|
||||
parantheses are added to the beginning and the end. Then the string can be interpreted by the
|
||||
reader as an actual lisp list. A nested lisp containing all sub-lists (lines) is returned.
|
||||
First line is assumed to be a comment (as no comment syntax is specified)."
|
||||
(let ((list nil))
|
||||
(with-open-file (input filename)
|
||||
(setf list
|
||||
(loop for line = (read-line input nil)
|
||||
while line collect (read-from-string
|
||||
(substitute #\Tab #\, (format nil "(~a)~%" line)))))
|
||||
;; throw away first line, which is assumed to be a comment
|
||||
(cdr list))))
|
||||
|
||||
(defun calc_sums (nested_list)
|
||||
"Return a list of sums of each sub-list in a nested list."
|
||||
(loop for sublist in nested_list collect (apply #'+ sublist)))
|
||||
|
||||
(defun list_to_csv (nested_list)
|
||||
"Converts the nested list back into a csv-formatted string."
|
||||
(substitute #\, #\
|
||||
(substitute #\newline #\)
|
||||
(remove #\((string-trim ")(" (format nil "~A" nested_list))))))
|
||||
;; main program
|
||||
;; prints the results as lisp lists and as csv
|
||||
(let ((nested_list (csv_to_nested_list "example_comma_csv.txt" #\,))
|
||||
(sum_list nil)
|
||||
(comment "#C1,C2,C3,C4,C5,SUM"))
|
||||
(setf sum_list (loop
|
||||
for list in nested_list
|
||||
for sum in (calc_sums nested_list)
|
||||
collect (append list (list sum))))
|
||||
(format t "~A~%~%" sum_list) ;; print nested list in lisp representation
|
||||
(format t "~A~%~A~%" comment (list_to_csv sum_list))) ;; print it again as csv
|
||||
10
Task/CSV-data-manipulation/D/csv-data-manipulation.d
Normal file
10
Task/CSV-data-manipulation/D/csv-data-manipulation.d
Normal 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)));
|
||||
}
|
||||
36
Task/CSV-data-manipulation/Erlang/csv-data-manipulation.erl
Normal file
36
Task/CSV-data-manipulation/Erlang/csv-data-manipulation.erl
Normal 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 ).
|
||||
49
Task/CSV-data-manipulation/Forth/csv-data-manipulation.fth
Normal file
49
Task/CSV-data-manipulation/Forth/csv-data-manipulation.fth
Normal 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
|
||||
;
|
||||
79
Task/CSV-data-manipulation/Go/csv-data-manipulation.go
Normal file
79
Task/CSV-data-manipulation/Go/csv-data-manipulation.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"encoding/csv"
|
||||
"io"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
func init() {
|
||||
log.SetFlags(log.Lshortfile)
|
||||
}
|
||||
|
||||
func main() {
|
||||
// Open the sample file given.
|
||||
csvFile, err := os.Open("sample.csv")
|
||||
|
||||
// Exit on error.
|
||||
if err != nil {
|
||||
log.Fatal("Error opening sample csv file:", err)
|
||||
}
|
||||
|
||||
// Make sure the file is closed before the function returns.
|
||||
defer csvFile.Close()
|
||||
|
||||
// Create a new csv reader for the file.
|
||||
csvReader := csv.NewReader(csvFile)
|
||||
|
||||
// Create an output file.
|
||||
outputFile, err := os.Create("output.csv")
|
||||
if err != nil {
|
||||
log.Fatal("Error creating output file:", err)
|
||||
}
|
||||
defer outputFile.Close()
|
||||
|
||||
csvWriter := csv.NewWriter(outputFile)
|
||||
defer csvWriter.Flush()
|
||||
|
||||
// For each row in the data.
|
||||
for i := 0; ; i++ {
|
||||
record, err := csvReader.Read()
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
log.Fatal("Error reading record:", err)
|
||||
}
|
||||
|
||||
// Skip header row.
|
||||
if i == 0 {
|
||||
err = csvWriter.Write(record)
|
||||
if err != nil {
|
||||
log.Fatal("Error writing record to output file:", err)
|
||||
}
|
||||
continue
|
||||
}
|
||||
|
||||
// For each cell in the row.
|
||||
for cell := range record {
|
||||
// Convert value to integer for manipulation.
|
||||
v, err := strconv.Atoi(record[cell])
|
||||
if err != nil {
|
||||
log.Fatal("Error parsing cell value:", err)
|
||||
}
|
||||
|
||||
// Do something to the value.
|
||||
v += 1
|
||||
// Store the new value back in the record variable.
|
||||
record[cell] = strconv.Itoa(v)
|
||||
}
|
||||
|
||||
// Write modified record to disk.
|
||||
err = csvWriter.Write(record)
|
||||
if err != nil {
|
||||
log.Fatal("Error writing record to output file:", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -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')
|
||||
34
Task/CSV-data-manipulation/Haskell/csv-data-manipulation.hs
Normal file
34
Task/CSV-data-manipulation/Haskell/csv-data-manipulation.hs
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
import Data.Array
|
||||
import Data.Maybe (isJust)
|
||||
import Data.List (intercalate)
|
||||
import Control.Monad (when)
|
||||
|
||||
delimiters = ",;:"
|
||||
|
||||
fields [] = []
|
||||
fields xs = let (item, rest) = break (`elem` delimiters) xs
|
||||
(_, next) = break (`notElem` delimiters) rest
|
||||
in item : fields next
|
||||
|
||||
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 [] = Nothing
|
||||
fieldArray xs = Just $ listArray ((1,1), (length xs, length $ head xs))
|
||||
$ concat xs
|
||||
|
||||
fieldsFromFile = fmap (fieldArray . map fields . lines) . readFile
|
||||
|
||||
fieldsToFile f = writeFile f . unlines . unfields
|
||||
|
||||
someChanges = fmap (// [((1,1), "changed"), ((3,4), "altered"),
|
||||
((5,2), "modified")])
|
||||
|
||||
main = do
|
||||
a <- fieldsFromFile "example.txt"
|
||||
when (isJust a) $ fieldsToFile "output.txt" $ someChanges a
|
||||
11
Task/CSV-data-manipulation/Icon/csv-data-manipulation.icon
Normal file
11
Task/CSV-data-manipulation/Icon/csv-data-manipulation.icon
Normal 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
|
||||
3
Task/CSV-data-manipulation/J/csv-data-manipulation-1.j
Normal file
3
Task/CSV-data-manipulation/J/csv-data-manipulation-1.j
Normal 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
|
||||
4
Task/CSV-data-manipulation/J/csv-data-manipulation-2.j
Normal file
4
Task/CSV-data-manipulation/J/csv-data-manipulation-2.j
Normal 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
|
||||
5
Task/CSV-data-manipulation/J/csv-data-manipulation-3.j
Normal file
5
Task/CSV-data-manipulation/J/csv-data-manipulation-3.j
Normal 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'
|
||||
2
Task/CSV-data-manipulation/J/csv-data-manipulation-4.j
Normal file
2
Task/CSV-data-manipulation/J/csv-data-manipulation-4.j
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
sumCSVrows=: writecsv~ (((<'SUM') ,~ {.) , [: (<"0)@(,. +/"1) makenum@}.)@readcsv
|
||||
'rc_out.csv' sumCSVrows 'rc.csv'
|
||||
104
Task/CSV-data-manipulation/Java/csv-data-manipulation-1.java
Normal file
104
Task/CSV-data-manipulation/Java/csv-data-manipulation-1.java
Normal 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) {
|
||||
}
|
||||
}
|
||||
}
|
||||
125
Task/CSV-data-manipulation/Java/csv-data-manipulation-2.java
Normal file
125
Task/CSV-data-manipulation/Java/csv-data-manipulation-2.java
Normal 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();
|
||||
}
|
||||
}
|
||||
31
Task/CSV-data-manipulation/Lua/csv-data-manipulation.lua
Normal file
31
Task/CSV-data-manipulation/Lua/csv-data-manipulation.lua
Normal 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)
|
||||
13
Task/CSV-data-manipulation/MATLAB/csv-data-manipulation.m
Normal file
13
Task/CSV-data-manipulation/MATLAB/csv-data-manipulation.m
Normal 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);
|
||||
|
|
@ -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);
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
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[[1, 1]] = Column0;
|
||||
iCSV[[2, 2]] = 100;
|
||||
iCSV[[3, 3]] = 200;
|
||||
iCSV[[4, 4]] = 300;
|
||||
iCSV[[5, 5]] = 400;
|
||||
iCSV[[2, 3]] = 60;
|
||||
Export["test.csv",iCSV];
|
||||
122
Task/CSV-data-manipulation/NetRexx/csv-data-manipulation.netrexx
Normal file
122
Task/CSV-data-manipulation/NetRexx/csv-data-manipulation.netrexx
Normal 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
|
||||
36
Task/CSV-data-manipulation/PHP/csv-data-manipulation.php
Normal file
36
Task/CSV-data-manipulation/PHP/csv-data-manipulation.php
Normal 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++;
|
||||
}
|
||||
?>
|
||||
65
Task/CSV-data-manipulation/PL-I/csv-data-manipulation.pli
Normal file
65
Task/CSV-data-manipulation/PL-I/csv-data-manipulation.pli
Normal 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;
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
my $csvfile = './whatever.csv';
|
||||
my $fh = open($csvfile, :r);
|
||||
my @header = $fh.get.split(',');
|
||||
my @csv = map {[.split(',')]}, $fh.lines;
|
||||
close $fh;
|
||||
|
||||
my $out = open($csvfile, :w);
|
||||
$out.say((@header,'SUM').join(','));
|
||||
$out.say((@$_, [+] @$_).join(',')) for @csv;
|
||||
close $out;
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
use Text::CSV;
|
||||
my $csvfile = './whatever.csv';
|
||||
my @csv = Text::CSV.parse-file($file);
|
||||
modify(@csv); # do whatever;
|
||||
csv-write-file( @csv, :file($csvfile) );
|
||||
38
Task/CSV-data-manipulation/Perl/csv-data-manipulation-1.pl
Normal file
38
Task/CSV-data-manipulation/Perl/csv-data-manipulation-1.pl
Normal 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;
|
||||
25
Task/CSV-data-manipulation/Perl/csv-data-manipulation-2.pl
Normal file
25
Task/CSV-data-manipulation/Perl/csv-data-manipulation-2.pl
Normal 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;
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
(in "data.csv"
|
||||
(prinl (line) "," "SUM")
|
||||
(while (split (line) ",")
|
||||
(prinl (glue "," @) "," (sum format @)) ) )
|
||||
22
Task/CSV-data-manipulation/Prolog/csv-data-manipulation.pro
Normal file
22
Task/CSV-data-manipulation/Prolog/csv-data-manipulation.pro
Normal 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].
|
||||
11
Task/CSV-data-manipulation/Python/csv-data-manipulation.py
Normal file
11
Task/CSV-data-manipulation/Python/csv-data-manipulation.py
Normal 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='')
|
||||
10
Task/CSV-data-manipulation/R/csv-data-manipulation-1.r
Normal file
10
Task/CSV-data-manipulation/R/csv-data-manipulation-1.r
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
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 <- transform(df,SUM = rowSums(df))
|
||||
|
||||
write.csv(df,row.names = FALSE)
|
||||
1
Task/CSV-data-manipulation/R/csv-data-manipulation-2.r
Normal file
1
Task/CSV-data-manipulation/R/csv-data-manipulation-2.r
Normal file
|
|
@ -0,0 +1 @@
|
|||
write.csv(df,row.names = FALSE,file = "foo.csv")
|
||||
1
Task/CSV-data-manipulation/README
Normal file
1
Task/CSV-data-manipulation/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/CSV_data_manipulation
|
||||
24
Task/CSV-data-manipulation/REXX/csv-data-manipulation.rexx
Normal file
24
Task/CSV-data-manipulation/REXX/csv-data-manipulation.rexx
Normal 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
|
||||
|
|
@ -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))))
|
||||
|
|
@ -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)))
|
||||
12
Task/CSV-data-manipulation/Ruby/csv-data-manipulation.rb
Normal file
12
Task/CSV-data-manipulation/Ruby/csv-data-manipulation.rb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
require 'csv'
|
||||
# read:
|
||||
ar = CSV.table("test.csv").to_a #table method assumes headers and converts numbers if possible.
|
||||
|
||||
# manipulate:
|
||||
ar.first << "SUM"
|
||||
ar[1..-1].each{|row| row << row.inject(:+)}
|
||||
|
||||
# write:
|
||||
CSV.open("out.csv", 'w') do |csv|
|
||||
ar.each{|line| csv << line}
|
||||
end
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
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
|
||||
"
|
||||
|
||||
print csv$
|
||||
dim csvData$(5,5)
|
||||
|
||||
for r = 1 to 5
|
||||
a$ = word$(csv$,r,chr$(13))
|
||||
for c = 1 to 5
|
||||
csvData$(r,c) = word$(a$,c,",")
|
||||
next c
|
||||
next r
|
||||
|
||||
[loop]
|
||||
input "Row to change:";r
|
||||
input "Col to change;";c
|
||||
if r > 5 or c > 5 then
|
||||
print "Row ";r;" or Col ";c;" is greater than 5"
|
||||
goto [loop]
|
||||
end if
|
||||
input "Change Row ";r;" Col ";c;" from ";csvData$(r,c);" to ";d$
|
||||
csvData$(r,c) = d$
|
||||
for r = 1 to 5
|
||||
for c = 1 to 5
|
||||
print cma$;csvData$(r,c);
|
||||
cma$ = ","
|
||||
next c
|
||||
cma$ = ""
|
||||
print
|
||||
next r
|
||||
28
Task/CSV-data-manipulation/Scala/csv-data-manipulation.scala
Normal file
28
Task/CSV-data-manipulation/Scala/csv-data-manipulation.scala
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
import scala.io.Source
|
||||
|
||||
object parseCSV extends App {
|
||||
|
||||
val rawData = """|C1,C2,C3,C4,C5
|
||||
|1,5,9,13,17
|
||||
|2,6,10,14,18
|
||||
|3,7,11,15,19
|
||||
|20,21,22,23,24""".stripMargin
|
||||
|
||||
val data = Seq((Source.fromString(rawData).getLines()).map(_.split(",")).toSeq: _*)
|
||||
|
||||
val output = ((data.take(1).flatMap(x => x) :+ "SUM").mkString(",") +: // Header line
|
||||
data.drop(1).map(_.map(_.toInt)). // Convert per line each array of String to array of integer
|
||||
map(cells => (cells, cells.sum)). //Add sum column to assemble a tuple. Part 1 are original numbers, 2 is the sum
|
||||
map(part => s"${part._1.mkString(",")},${part._2}")).mkString("\n")
|
||||
|
||||
println(output)
|
||||
/* Outputs:
|
||||
|
||||
C1,C2,C3,C4,C5,SUM
|
||||
1,5,9,13,17,45
|
||||
2,6,10,14,18,50
|
||||
3,7,11,15,19,55
|
||||
20,21,22,23,24,110
|
||||
|
||||
*/
|
||||
}
|
||||
17
Task/CSV-data-manipulation/Seed7/csv-data-manipulation.seed7
Normal file
17
Task/CSV-data-manipulation/Seed7/csv-data-manipulation.seed7
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var file: input is STD_NULL;
|
||||
var array array string: csvData is 0 times 0 times "";
|
||||
var integer: line is 0;
|
||||
begin
|
||||
input := open(dir(PROGRAM) & "/csvDataManipulation.in", "r");
|
||||
while hasNext(input) do
|
||||
csvData &:= split(getln(input), ",");
|
||||
end while;
|
||||
csvData[3][3] := "X";
|
||||
for key line range csvData do
|
||||
writeln(join(csvData[line], ","));
|
||||
end for;
|
||||
end func;
|
||||
18
Task/CSV-data-manipulation/TUSCRIPT/csv-data-manipulation.tu
Normal file
18
Task/CSV-data-manipulation/TUSCRIPT/csv-data-manipulation.tu
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
$$ MODE 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
|
||||
$$ MODE TUSCRIPT
|
||||
LOOP/CLEAR n,line=csv
|
||||
IF (n==1) THEN
|
||||
line=CONCAT (line,",SUM")
|
||||
ELSE
|
||||
lineadd=EXCHANGE(line,":,:':")
|
||||
sum=SUM(lineadd)
|
||||
line=JOIN(line,",",sum)
|
||||
ENDIF
|
||||
csv=APPEND(csv,line)
|
||||
ENDLOOP
|
||||
29
Task/CSV-data-manipulation/Tcl/csv-data-manipulation.tcl
Normal file
29
Task/CSV-data-manipulation/Tcl/csv-data-manipulation.tcl
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
package require struct::matrix
|
||||
package require csv
|
||||
|
||||
proc addSumColumn {filename {title "SUM"}} {
|
||||
set m [struct::matrix]
|
||||
|
||||
# Load the CSV in
|
||||
set f [open $filename]
|
||||
csv::read2matrix $f $m "," auto
|
||||
close $f
|
||||
|
||||
# Add the column with the sums
|
||||
set sumcol [$m columns]
|
||||
$m add column $title
|
||||
for {set i 1} {$i < [$m rows]} {incr i} {
|
||||
# Fill out a dummy value
|
||||
$m set cell $sumcol $i 0
|
||||
$m set cell $sumcol $i [tcl::mathop::+ {*}[$m get row $i]]
|
||||
}
|
||||
|
||||
# Write the CSV out
|
||||
set f [open $filename w]
|
||||
csv::writematrix $m $f
|
||||
close $f
|
||||
|
||||
$m destroy
|
||||
}
|
||||
|
||||
addSumColumn "example.csv"
|
||||
|
|
@ -0,0 +1,20 @@
|
|||
exec 0<"$1" # open the input file on stdin
|
||||
exec 1>"$1.new" # open an output file on stdout
|
||||
{
|
||||
read -r header
|
||||
echo "$header,SUM"
|
||||
IFS=,
|
||||
while read -r -a numbers; do
|
||||
sum=0
|
||||
for num in "${numbers[@]}"; do
|
||||
(( sum += num ))
|
||||
done
|
||||
|
||||
# can write the above loop as
|
||||
# sum=$(( $(IFS=+; echo "${numbers[*]}") ))
|
||||
|
||||
echo "${numbers[*]},$sum"
|
||||
done
|
||||
} &&
|
||||
mv "$1" "$1.bak" &&
|
||||
mv "$1.new" "$1"
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
File_Open("input.csv")
|
||||
for (#1 = 0; #1 < 4; #1++) {
|
||||
Goto_Line(#1+2) // line (starting from line 2)
|
||||
if (#1) {
|
||||
Search(",", ADVANCE+COUNT, #1) // column
|
||||
}
|
||||
#2 = Num_Eval() // #2 = old value
|
||||
Del_Char(Chars_Matched) // delete old value
|
||||
Num_Ins(#2+100, LEFT+NOCR) // write new value
|
||||
}
|
||||
File_Save_As("output.csv", OK+NOMSG)
|
||||
Loading…
Add table
Add a link
Reference in a new issue