How Do I Ignore Blank Lines Processing a CSV File In Clojure?

THX1137

How do I ignore blank lines when mapping zipmap over a file?

(defn csv-data->maps [csv-data]
  (map zipmap
       (->> (first csv-data) ;; First row is the header
            repeat)
       (rest csv-data)))
Alan Thompson

Easiest way is to re-use an existing library:

(ns tst.demo.core
  (:use tupelo.core tupelo.test)
  (:require
    [clojure.string :as str]
    [schema.core :as s]
    [tupelo.csv :as csv]))

(s/defn remove-blank-lines :- s/Str
  "Accepts a multi-line text string, and returns one with any blank lines removed."
  [text-str :- s/Str]
  (let [text-lines      (str/split-lines text-str)
        lines-no-blanks (remove str/blank? text-lines)
        text-no-blanks  (str/join \newline lines-no-blanks)]
    text-no-blanks))

(dotest
  (let [csv-text           "zip-postal-code,store-num,chain-rank
                            01002,00006,4
                            01002,00277,5

                            01003,00277,5
                            01008,01217,5
                            01009,00439,5
                            01020,01193,5"
        csv-text-no-blanks (remove-blank-lines csv-text)
        csv-entities       (csv/parse->entities csv-text-no-blanks)
        csv-attrs          (csv/entities->attrs csv-entities)]
    (is= csv-entities
      [{:zip-postal-code "01002", :store-num "00006", :chain-rank "4"}
       {:zip-postal-code "01002", :store-num "00277", :chain-rank "5"}
       {:zip-postal-code "01003", :store-num "00277", :chain-rank "5"}
       {:zip-postal-code "01008", :store-num "01217", :chain-rank "5"}
       {:zip-postal-code "01009", :store-num "00439", :chain-rank "5"}
       {:zip-postal-code "01020", :store-num "01193", :chain-rank "5"}])

As the example shows, you can get the CSV data either row-oriented (entity maps) or column oriented (attribute vectors).

    (is= csv-attrs
      {:store-num       ["00006" "00277" "00277" "01217" "00439" "01193"],
       :zip-postal-code ["01002" "01002" "01003" "01008" "01009" "01020"],
       :chain-rank      ["4" "5" "5" "5" "5" "5"]})
    ))

See the docs here for the tupelo.csv lib.


Another way (perhaps easier) is to pre-process the file with a simple Unix tool like sed. Consider a sample file:

~/expr/demo > cat csv.txt                
zip-postal-code,store-num,chain-rank

01002,00006,4
01002,00277,5

01003,00277,5
01008,01217,5
01009,00439,5
01020,01193,5

and process it with sed (Stream EDitor):

~/expr/demo > sed  '/^ *$/d'  csv.txt   
zip-postal-code,store-num,chain-rank
01002,00006,4
01002,00277,5
01003,00277,5
01008,01217,5
01009,00439,5
01020,01193,5

Viola!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I split a text file into an array by blank lines?

How to ignore lines in CSV file with a specific value?

How can I delete blank lines in a file?

How do I count number of lines in an imported to array CSV file?

Do all browsers ignore blank lines in html

How to add alternating blank lines when saving csv file in Pandas

Ignore processing lines which start with # in batch file

How do I read in text from file, but ignore certain lines in C#?

How to ignore all lines after a specific word on processing lines of a text file in a FOR loop?

How can I open csv file in powershell and remove lines with blank data, change column order and write out results

How do I remove blank lines from a string in Python?

VSCode: How do I KEEP blank lines (in scss files or others)?

Is it possible to Skip Blank Lines in a Dataframe? If Yes then how I can do this

How do I remove blank lines from files?

How do I process a CSV file in scala so that I can separate its lines into arrays?

How do I ignore long lines in Sublime Text when searching

How do I remove duplicate lines and ignore some of the text?

Read CSV file in Pandas with Blank lines in between

Blank lines when reading CSV file in Java

removing header and blank lines from a csv file

Remove blank lines in CSV file with GEANY

Perl deleting "blank" lines from a csv file

How do I launch an exe file in processing?

How do I read a .csv file in Java with some cells containing multiple lines?

Easiest way to ignore blank lines when reading a file in Python

Bash Scripting Ignore blank lines + comments in a passed through file

How do I fetch lines in a log file

How do I comment lines in .plist file?

how do I open file to print lines?