こちらと同様のことを wrtn で行いました。
ChatGPT で elixir を学ぶ (その7)text ファイルの delete
リートンでは、Map の削除のスニペットを作成しました。(Python と同等の機能のプログラムは作成できませんでした。)
Python のプログラム
text_delete.py
#! /usr/bin/python
#
# delete/text_delete.py
#
# Mar/07/2024
import sys
#
# ---------------------------------------------------------------
def dict_delete_proc(dict_in,key):
if key in dict_in:
del dict_in[key]
#
return dict_in
#
# ---------------------------------------------------------------
def text_write_proc(file_out,dict_aa):
#
fp_out = open(file_out,mode='w', encoding='utf-8')
for key in dict_aa.keys():
unit = dict_aa[key]
str_out = key + "\t" + str(unit['name']) + "\t"
str_out += "%d\t" % unit['population']
str_out += unit['date_mod'] + "\n"
fp_out.write(str_out)
fp_out.close()
# ---------------------------------------------------------------
def text_read_proc(file_in):
#
fp_in = open(file_in,encoding='utf-8')
lines = fp_in.readlines()
fp_in.close()
#
dict_aa = {}
for line in lines:
if (5 < len(line)):
cols= line[:-1].split('\t')
if (3 < len(cols)):
try:
if (cols[0][0] == "t"):
dict_unit = {'name': cols[1], \
'population':int (cols[2]),'date_mod':cols[3]}
dict_aa[cols[0]] = dict_unit
except:
sys.stderr.write \
("*** error *** %s ***\n" % file_in)
sys.stderr.write \
("*** %s ***\n" % line)
#
return dict_aa
# ---------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_in = sys.argv[1]
id_in = sys.argv[2]
print("%s" % id_in)
dict_aa = text_read_proc(file_in)
dict_bb = dict_delete_proc(dict_aa,id_in)
text_write_proc(file_in,dict_bb)
#
sys.stderr.write("*** 終了 ***\n")
# ---------------------------------------------------------------
リートンで得たプログラム
def delete_from_dict(dict, key) do
Map.delete(dict, key)
end
こちらの読み込みプログラム
リートン で elixir を学ぶ (その3)text ファイルの read
こちらの書き込みプログラム
リートン で elixir を学ぶ (その2)text ファイルの write
text_delete.exs
#! /usr/bin/elixir
defmodule TextDelete do
def read_file(file_path) do
file_path
|> File.stream!()
|> Enum.reduce(%{}, fn line, acc ->
process_line(line, acc)
end)
end
defp process_line(line, acc) do
[key, name, population, date_mod | _] = String.split(line, "\t")
if String.starts_with?(key, "t") do
Map.put(acc, key, %{
name: name,
population: String.to_integer(population),
date_mod: String.trim_trailing(date_mod)
})
else
acc
end
end
def delete_from_dict(dict, key) do
Map.delete(dict, key)
end
def text_write_proc(file_out, dict_aa) do
{:ok, file} = File.open(file_out, [:write, :utf8])
dict_aa
|> Enum.each(fn {key, val} ->
str_out = "#{key}\t#{val.name}\t#{val.population}\t#{val.date_mod}\n"
IO.write(file, str_out)
end)
File.close(file)
end
end
# -------------------------------------------------------------------------
IO.puts :stderr,"*** 開始 ***"
[file_in, id_in] = System.argv()
IO.puts :stderr,file_in
IO.puts("#{id_in}")
dict_aa = TextDelete.read_file(file_in)
dict_bb = TextDelete.delete_from_dict(dict_aa, id_in)
# IO.inspect(dict_bb)
TextDelete.text_write_proc(file_in, dict_bb)
IO.puts :stderr,"*** 終了 ***"
# -------------------------------------------------------------------------
実行結果
$ ./text_delete.exs cities.txt t2385
t2385
*** 開始 ***
cities.txt
*** 終了 ***
を使って、次のプログラムを作成しました。