September Morn Update
This commit is contained in:
parent
4e2d22a71d
commit
aac6731f2c
6856 changed files with 141342 additions and 21127 deletions
1
Task/CSV-data-manipulation/00META.yaml
Normal file
1
Task/CSV-data-manipulation/00META.yaml
Normal file
|
|
@ -0,0 +1 @@
|
|||
--- {}
|
||||
|
|
@ -1,34 +1,45 @@
|
|||
import Data.Array
|
||||
import Data.Maybe (isJust)
|
||||
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
|
||||
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
|
||||
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
|
||||
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 = fmap (// [((1,1), "changed"), ((3,4), "altered"),
|
||||
((5,2), "modified")])
|
||||
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
|
||||
a <- fieldsFromFile "example.txt"
|
||||
when (isJust a) $ fieldsToFile "output.txt" $ someChanges a
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
}
|
||||
22
Task/CSV-data-manipulation/Python/csv-data-manipulation-2.py
Normal file
22
Task/CSV-data-manipulation/Python/csv-data-manipulation-2.py
Normal 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)
|
||||
|
|
@ -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)
|
||||
3
Task/CSV-data-manipulation/Q/csv-data-manipulation.q
Normal file
3
Task/CSV-data-manipulation/Q/csv-data-manipulation.q
Normal 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
|
||||
19
Task/CSV-data-manipulation/REXX/csv-data-manipulation-2.rexx
Normal file
19
Task/CSV-data-manipulation/REXX/csv-data-manipulation-2.rexx
Normal 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.*/
|
||||
28
Task/CSV-data-manipulation/Rust/csv-data-manipulation.rust
Normal file
28
Task/CSV-data-manipulation/Rust/csv-data-manipulation.rust
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
use std::error::Error;
|
||||
use std::num::ParseIntError;
|
||||
use csv::{Reader, Writer};
|
||||
|
||||
fn main() -> Result<(), Box<dyn Error>> {
|
||||
let mut reader = Reader::from_path("data.csv")?;
|
||||
let mut writer = Writer::from_path("output.csv")?;
|
||||
|
||||
// headers() returns an immutable reference, so clone() before appending
|
||||
let mut headers = reader.headers()?.clone();
|
||||
headers.push_field("SUM");
|
||||
writer.write_record(headers.iter())?;
|
||||
|
||||
for row in reader.records() {
|
||||
let mut row = row?;
|
||||
|
||||
// `sum` needs the type annotation so that `parse::<i64>` knows what error type to return
|
||||
let sum: Result<_, ParseIntError> = row.iter().try_fold(0, |accum, s| {
|
||||
Ok(accum + s.parse::<i64>()?)
|
||||
});
|
||||
|
||||
row.push_field(&sum?.to_string());
|
||||
writer.write_record(row.iter())?;
|
||||
}
|
||||
|
||||
writer.flush()?;
|
||||
Ok(())
|
||||
}
|
||||
|
|
@ -1,4 +1,5 @@
|
|||
import delim input.csv, clear
|
||||
replace c5=c3+c4
|
||||
egen sum=rowtotal(c*)
|
||||
drop if mod(c3,3)==0
|
||||
export delim output.csv, replace
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue