Julia: text ファイルの crud

#共通の関数#

```julia:text_manipulate.jl
#
#	text_manipulate.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
function text_to_dict_proc(fname_in)
	delimiter = "\t"
	dict_aa = text_to_dict_proc_exec(delimiter,fname_in)
#
	return	dict_aa
end
# --------------------------------------------------------------------
function text_to_dict_proc_exec(delimiter,fname_in)
	ff = open(fname_in, "r")
	string = read(ff, String)
	close(ff)
	lines = split(string,"\n")
#
	dict_aa = Dict()
	llx = length(lines)
	println(llx)
	for it=1:llx
		line = lines[it]
		if (1 < length(line))
			cols = split(line,delimiter)
			key = cols[1]
			K = ["name","population","date_mod"]
			V = [cols[2],cols[3],cols[4]]
#			unit_aa = Dict(K,V)
			unit_aa = Dict("name" => cols[2],"population" => cols[3], "date_mod" => cols[4])
			dict_aa[key] = unit_aa
		end
	end
#
	return	dict_aa
end
# --------------------------------------------------------------------
function dict_display_proc(dict_aa)
#
	for key in sort(collect(keys(dict_aa)))
		print(key, '\t', dict_aa[key]["name"])
		print('\t', dict_aa[key]["population"])
		println('\t', dict_aa[key]["date_mod"])
	end

end
# --------------------------------------------------------------------
function dict_append_proc(dict_aa,key,name,population,date_mod)
	K = ["name","population","date_mod"]
	V = [name,population,date_mod]
#	unit_aa = Dict(K,V)
	unit_aa = Dict("name" => name,"population" => population, "date_mod" => date_mod)
	dict_aa[key] = unit_aa
#
	return	dict_aa
end
# --------------------------------------------------------------------
function dict_to_text_proc(dict_aa,fname_out)
	delimiter = "\t"
	dict_to_text_proc_exec(delimiter,dict_aa,fname_out)
#
end
#
# --------------------------------------------------------------------
function dict_to_text_proc_exec(delimiter,dict_aa,fname_out)
	fp_out = open(fname_out,"w")
#
	for key in sort(collect(keys(dict_aa)))
		str_out = string(key,delimiter,dict_aa[key]["name"])
		str_out = string(str_out,delimiter,dict_aa[key]["population"])
		str_out = string(str_out,delimiter,dict_aa[key]["date_mod"])
		str_out = string(str_out,'\n')
		write(fp_out,str_out)
	end
#
	close(fp_out)
end
#
# --------------------------------------------------------------------
function dict_update_proc(dict_aa,key,population_in)
#
	today = Libc.strftime("%F", time())
#
	unit_aa = Dict()
	unit_aa["name"] = dict_aa[key]["name"]
	unit_aa["population"] = population_in
	unit_aa["date_mod"] = today
	dict_aa[key] = unit_aa
#
	return	dict_aa
end
#
# --------------------------------------------------------------------
function csv_to_dict_proc(fname_in)
	delimiter = ","
	dict_aa = text_to_dict_proc_exec(delimiter,fname_in)
#
	return	dict_aa
end
# --------------------------------------------------------------------
function dict_to_csv_proc(dict_aa,fname_out)
	delimiter = ","
	dict_to_text_proc_exec(delimiter,dict_aa,fname_out)
#
end
#
# --------------------------------------------------------------------
```

#Create#

```julia:text_create.jl
#! /usr/bin/julia
#
#	text_create.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
include("text_manipulate.jl")
#
# --------------------------------------------------------------------
function data_prepare_proc()
	dict_aa = Dict()
#
	dict_aa = dict_append_proc(dict_aa,"t2381","名古屋",32761,"2013-5-30")
	dict_aa = dict_append_proc(dict_aa,"t2382","豊橋",87295,"2013-9-10")
	dict_aa = dict_append_proc(dict_aa,"t2383","岡崎",21674,"2013-2-14")
	dict_aa = dict_append_proc(dict_aa,"t2384","一宮",73612,"2013-3-9")
	dict_aa = dict_append_proc(dict_aa,"t2385","蒲郡",52391,"2013-8-4")
	dict_aa = dict_append_proc(dict_aa,"t2386","常滑",95187,"2013-1-21")
	dict_aa = dict_append_proc(dict_aa,"t2387","大府",31246,"2013-7-23")
	dict_aa = dict_append_proc(dict_aa,"t2388","瀬戸",85791,"2013-11-26")
	dict_aa = dict_append_proc(dict_aa,"t2389","犬山",24139,"2013-10-15")
#
	
	return	dict_aa
end
# --------------------------------------------------------------------
println("*** 開始 ***")
fname_out = ARGS[1]
println(fname_out)
#
dict_aa = data_prepare_proc()
# dict_display_proc(dict_aa)
dict_to_text_proc(dict_aa,fname_out)
#
println("*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./text_create.jl /var/tmp/plain_text/cities.txt
```

#Read#

```julia:text_read.jl
#! /usr/bin/julia
#
#	text_read.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
include("text_manipulate.jl")
#
# --------------------------------------------------------------------
println("*** 開始 ***")
fname_in = ARGS[1]
println(fname_in)
#
dict_aa = text_to_dict_proc(fname_in)
dict_display_proc(dict_aa)
#
println("*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./text_read.jl /var/tmp/plain_text/cities.txt
```

#Update#

```julia:text_update.jl
#! /usr/bin/julia
#
#	text_update.jl
#
#					Dec/22/2021
# --------------------------------------------------------------------
include("text_manipulate.jl")
#
# --------------------------------------------------------------------
println("*** 開始 ***")
fname_text = ARGS[1]
key_in = ARGS[2]
population_in = ARGS[3]
println(fname_text)
println(key_in,'\t',population_in)
#
dict_aa = text_to_dict_proc(fname_text)
#
if ( haskey(dict_aa,key_in))
	dict_aa = dict_update_proc(dict_aa,key_in,population_in)
	dict_to_text_proc(dict_aa,fname_text)
end
#
println("*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./text_update.jl /var/tmp/plain_text/cities.txt t2385 45982100
```


#Delete#

```julia:text_delete.jl
#! /usr/bin/julia
#
#	text_delete.jl
#
#					Jul/05/2018
# --------------------------------------------------------------------
include("text_manipulate.jl")
#
# --------------------------------------------------------------------
println("*** 開始 ***")
fname_text = ARGS[1]
key_in = ARGS[2]
println(fname_text)
println(key_in)
#
dict_aa = text_to_dict_proc(fname_text)
#
if ( haskey(dict_aa,key_in))
	delete!(dict_aa, key_in)
	dict_to_text_proc(dict_aa,fname_text)
end
#
println("*** 終了 ***")
# --------------------------------------------------------------------
```

実行コマンド

```bash
./text_delete.jl /var/tmp/plain_text/cities.txt t2382
```