你如何从一个文件中读取,然后将该文件分割成多个文件与Clojure?



我有一个文本文件,需要按以下方式分割:第一行是一个标识符,必须在每个创建的文件的顶部,以下行是一对由逗号分隔的数字:

例子

1234512000年,213000年,512501年,1等等…

当脚本运行时,它以以下方式留下3个文件

p1.dat 12345 12000,2等

p2.dat 12345 13000,5等…

p3.dat 12345 12501,1等

我已经在Ruby中做了一个脚本但是我对Clojure很信任并且/或者可能永远不会正确地学习它,请问有人能给一些启示吗?如何以一种"功能性"的方式做到这一点?Ruby代码:
#!/usr/bin/env ruby
if ARGV.length < 1
puts """To use this script give the path to the file and the number of parts to divide,
if you dont give this number 2 is assumed"""
exit
end
file_path = ARGV[0].to_s
if ARGV.length == 1
n_parts = 2
else
n_parts = ARGV[1].to_i
end
particiones = Array.new(n_parts)
j = Array(1..n_parts)
File.readlines(file_path).each_with_index do |linea, i|
#first line is id, so it must be in every divided part
if i == 0 #repeat first line in every file
j.each do |particion|
particiones[particion-1] = linea
end
next # After processing first line
end
particiones[(i-1)%n_partes] +=  linea # parts get stored
end
j.each do |particion| # files are written
# Crear los archivos p1, p2,... etc
File.open("p"+particion.to_s+".dat", 'w'){|f| f << particiones[particion-1]}
puts "File p"+particion.to_s+".dat was created"
end

我希望我正确理解了你的任务-试试这个:

代码:

(defn split-file
"First argument is path to file,
second argument is number of created files, default is 2"
([path] (split-file path 2))
([path parts]
(let [numbers (-> (slurp path)
(clojure.string/replace #"n|rn" " ")
(clojure.string/split #" "))
identifier (first numbers)
pairs (rest numbers)
file-names (for [i (range 1 (inc parts))]
(str "p" i ".dat"))
data-for-files (->> (iterate rest pairs)
(take parts)
(map #(take-nth parts %))
(map #(conj % identifier))
(map #(clojure.string/join "n" %)))]
(doall (map (fn [file-name text]
(spit file-name text))
file-names
data-for-files)))))

调用:

(split-file "data.txt" 3)

(它返回(nil nil nil),但这无关紧要)

Filedata.txt:

12345
12000,2
13000,5
12501,1
12000,2
13000,5
12501,1
12000,2
13000,5
12501,1

输出文件:

p1.dat:

12345
12000,2
12000,2
12000,2

p2.dat:

12345
13000,5
13000,5
13000,5

p3.dat:

12345
12501,1
12501,1
12501,1

最新更新