1

我的目标是将我的表格打印得尽可能接近,每个列之间有均匀的空格。

(defn PrintTable [tableName]
  "prints table in clear format"
  (let [tableRef (get (deref dataBase) tableName) ; get refrence for table
        keyList (keys @tableRef)] ; get key list of table
    (doseq [tableKeys (range (count keyList))] ; print the keys of the table
      (let [key (nth (keys @tableRef) tableKeys)]
        (print key "\t|"))
    )
    (println)
    (doseq [rows (range (count @(tableRef (nth (keys @tableRef) 0))))] ; print for each rows all the values
      (doseq [cols (range (count keyList))]
        (let [key (nth (keys @tableRef) cols)]
          (print (@(tableRef key) rows) "\t|")
        )
      )
      (println)
    )
  )
  (println)
)

我试过使用标签,但这是我得到的结果:

P_Id    |LastName   |FirstName  |Address    |City   |
1   |Darmon     |Gilad  |ishayahu   |Haifa  |
2   |SM     |Shiran     |erez   |RamatIshay     |

D_Id    |Name   |OwnerLastName  |OwnerFirstName     |
a   |Bono   |Darmon     |Gilad  |
b   |Bony   |SM     |Shiran     |

对更好和对齐的打印有什么建议吗?

4

4 回答 4

3

用于format使 cols 对齐:

user> (println (format "%20s %20s %20s\n%20s %20s %20s" 
                 "short" "medium" "reallylong" 
                 "reallylong" "medium" "short"))

               short               medium           reallylong
          reallylong               medium                short
nil
user> 

或左对齐%-20s

user> (println (format "%-20s %-20s %-20s\n%-20s %-20s %-20s" 
                        "short" "medium" "reallylong" 
                        "reallylong" "medium" "short")) 

short                medium               reallylong 
reallylong           medium               short 
nil 
user>
于 2013-02-22T18:49:28.987 回答
2

这可能会有所帮助:

http://clojuredocs.org/clojure_core/clojure.pprint/print-table

阿尔法 - 可能会发生变化。在文本表中打印一组地图。打印表标题 ks,然后每行输出一行,对应于 ks 中的键。如果未指定 ks,则使用行中第一项的键。

于 2013-06-18T11:20:29.740 回答
1
(defn print-table [res]
  (let [headers (map name (keys (first res)))
        table (concat [headers] (map vals res))
        trans-table (apply map vector table)
        cols-width (map #(apply max (map (comp count str) %))
                        trans-table)]
    (doseq [row table]
      (println
       (apply format
              (str "|" (apply str (str/join "|" (map #(str "%-" % "s")
                                                     cols-width)))
                   "|")
              row)))))

(print-table res)
=> |P_Id|LastName|FirstName|Address |City      |
   |1   |Darmon  |Gilad    |ishayahu|Haifa     |
   |2   |SM      |Shiran   |erez    |RamatIshay|
于 2013-02-22T19:40:55.110 回答
0

我习惯print-tablewith-out-str

user=> (def res [{:P_Id 1 :LastName "Darmon" :FirstName "Gilad"} {:P_Id 2 :LastName "SM" :FirstName "Shiran"}])
#'user/res
user=> (printf "res=%s" (with-out-str (clojure.pprint/print-table res)))
res=
| :P_Id | :LastName | :FirstName |
|-------+-----------+------------|
|     1 |    Darmon |      Gilad |
|     2 |        SM |     Shiran |
nil
于 2021-07-10T13:15:25.060 回答