How to resolve the algorithm CSV data manipulation step by step in the Clojure programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm CSV data manipulation step by step in the Clojure programming language

Table of Contents

Problem Statement

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.

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: Suggestions

Let's start with the solution:

Step by Step solution about How to resolve the algorithm CSV data manipulation step by step in the Clojure programming language

Source code in the clojure programming language

(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)))))


(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")


  

You may also check:How to resolve the algorithm Character codes step by step in the COBOL programming language
You may also check:How to resolve the algorithm Sierpinski carpet step by step in the 11l programming language
You may also check:How to resolve the algorithm OpenGL step by step in the Mathematica / Wolfram Language programming language
You may also check:How to resolve the algorithm Averages/Pythagorean means step by step in the MATLAB programming language
You may also check:How to resolve the algorithm URL parser step by step in the V (Vlang) programming language