Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-1.adb
Normal file
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-1.adb
Normal 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;
|
||||
24
Task/CSV-data-manipulation/Ada/csv-data-manipulation-2.adb
Normal file
24
Task/CSV-data-manipulation/Ada/csv-data-manipulation-2.adb
Normal 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;
|
||||
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-3.adb
Normal file
19
Task/CSV-data-manipulation/Ada/csv-data-manipulation-3.adb
Normal 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;
|
||||
93
Task/CSV-data-manipulation/COBOL/csv-data-manipulation.cob
Normal file
93
Task/CSV-data-manipulation/COBOL/csv-data-manipulation.cob
Normal 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.
|
||||
91
Task/CSV-data-manipulation/Euphoria/csv-data-manipulation.eu
Normal file
91
Task/CSV-data-manipulation/Euphoria/csv-data-manipulation.eu
Normal 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()
|
||||
|
|
@ -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
|
||||
|
|
@ -0,0 +1,9 @@
|
|||
import polars as pl
|
||||
|
||||
filepath = "data.csv"
|
||||
|
||||
(
|
||||
pl.scan_csv(filepath)
|
||||
.with_columns(pl.sum_horizontal(pl.all()).alias("SUM"))
|
||||
.sink_csv(filepath)
|
||||
)
|
||||
35
Task/CSV-data-manipulation/Rebol/csv-data-manipulation.rebol
Normal file
35
Task/CSV-data-manipulation/Rebol/csv-data-manipulation.rebol
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
Rebol [
|
||||
title: "Rosetta code: CSV data manipulation"
|
||||
file: %CSV_data_manipulation.r3
|
||||
url: https://rosettacode.org/wiki/CSV_data_manipulation
|
||||
]
|
||||
|
||||
write %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} ;; create a sample CSV file on disk
|
||||
|
||||
probe-csv: func [data][
|
||||
foreach row data [probe row] ;; helper: print each parsed CSV row using probe
|
||||
]
|
||||
|
||||
import csv ;; enable CSV codec for load/save of CSV files
|
||||
|
||||
data: load %test.csv ;; load CSV into a block of rows (row 1 is header)
|
||||
print "^/Original:" probe-csv data ;; show original data
|
||||
|
||||
;; Add SUM column:
|
||||
append data/1 "SUM" ;; extend header with a new column "SUM"
|
||||
foreach row next data [ ;; iterate over data rows (skip header)
|
||||
sum: 0
|
||||
forall row [ ;; walk each cell in the row in place
|
||||
change row to integer! row/1 ;; convert current cell to integer (mutates row)
|
||||
sum: sum + row/1 ;; accumulate the running total
|
||||
]
|
||||
append row sum ;; append the computed sum at the end of the row
|
||||
]
|
||||
print "^/Modified:" probe-csv data ;; show data after adding the SUM column
|
||||
|
||||
save %new.csv data ;; save the modified data to a new CSV file
|
||||
print "^/Reloaded:" probe-csv load %new.csv ;; reload and display to verify persistence
|
||||
|
|
@ -7,5 +7,5 @@ $ 2,6,10,14,18
|
|||
$ 3,7,11,15,19
|
||||
$ 4,8,12,16,20
|
||||
|
||||
&p⍜°csv(⍜⍉⊂:⊂□"SUM"≡(□⍜⋕/+)↘1.)
|
||||
&p⍜°csv(⍜⍉˜⊂⊂□"SUM"≡(□⍜⋕/+)⊸↘1)
|
||||
# Save using `&fwa "example.csv"`
|
||||
|
|
|
|||
|
|
@ -0,0 +1,35 @@
|
|||
'Instatiate FSO.
|
||||
Set objFSO = CreateObject("Scripting.FileSystemObject")
|
||||
'Open the CSV file for reading. The file is in the same folder as the script and named csv_sample.csv.
|
||||
Set objInCSV = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\csv_sample.csv",1,False)
|
||||
'Set header status to account for the first line as the column headers.
|
||||
IsHeader = True
|
||||
'Initialize the var for the output string.
|
||||
OutTxt = ""
|
||||
'Read each line of the file.
|
||||
Do Until objInCSV.AtEndOfStream
|
||||
line = objInCSV.ReadLine
|
||||
If IsHeader Then
|
||||
OutTxt = OutTxt & line & ",SUM" & vbCrLf
|
||||
IsHeader = False
|
||||
Else
|
||||
OutTxt = OutTxt & line & "," & AddElements(line) & vbCrLf
|
||||
End If
|
||||
Loop
|
||||
'Close the file.
|
||||
objInCSV.Close
|
||||
'Open the same file for writing.
|
||||
Set objOutCSV = objFSO.OpenTextFile(objFSO.GetParentFolderName(WScript.ScriptFullName) & "\csv_sample.csv",2,True)
|
||||
'Write the var OutTxt to the file overwriting existing contents.
|
||||
objOutCSV.Write OutTxt
|
||||
'Close the file.
|
||||
objOutCSV.Close
|
||||
Set objFSO = Nothing
|
||||
|
||||
'Routine to add each element in a row.
|
||||
Function AddElements(s)
|
||||
arr = Split(s,",")
|
||||
For i = 0 To UBound(arr)
|
||||
AddElements = AddElements + CInt(arr(i))
|
||||
Next
|
||||
End Function
|
||||
Loading…
Add table
Add a link
Reference in a new issue