2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

ChatGPT で elixir を学ぶ (その4)text ファイルの write

Last updated at Posted at 2024-03-03

こちらの記事の続きです。
ChatGPT で elixir を学ぶ (その3)

テキストファイルの作成をします。

Python の 辞書のデータをテキストに出力します。

Python のプログラム

text_create.py
#! /usr/bin/python
#
import	sys
#
# -------------------------------------------------------------------------
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 dict_append_proc(dict_aa,key,name,population,date_mod):
	dict_aa[key] = {'name':name,'population':population,'date_mod':date_mod}
#
	return dict_aa
#
# -------------------------------------------------------------------------
def	data_prepare_proc():
	dict_aa = {} 
#
	dict_aa = dict_append_proc(dict_aa,'t2381','名古屋',52761,'2003-4-30')
	dict_aa = dict_append_proc(dict_aa,'t2382','豊橋',47195,'2003-5-10')
	dict_aa = dict_append_proc(dict_aa,'t2383','岡崎',21674,'2003-6-14')
	dict_aa = dict_append_proc(dict_aa,'t2384','一宮',83912,'2003-9-9')
	dict_aa = dict_append_proc(dict_aa,'t2385','蒲郡',42791,'2003-8-4')
	dict_aa = dict_append_proc(dict_aa,'t2386','常滑',35287,'2003-1-21')
	dict_aa = dict_append_proc(dict_aa,'t2387','大府',81246,'2003-7-23')
	dict_aa = dict_append_proc(dict_aa,'t2388','瀬戸',25791,'2003-10-26')
	dict_aa = dict_append_proc(dict_aa,'t2389','犬山',54139,'2003-12-15')
#
	return	dict_aa
#
# -------------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
file_out = sys.argv[1]
#
print(file_out)
#
dict_aa = data_prepare_proc()
#
try:
	text_write_proc(file_out,dict_aa)
except Exception as ee:
	sys.stderr.write("*** error *** in text_write_proc ***\n")
	sys.stderr.write(str(ee) + '\n')
#
sys.stderr.write("*** 終了 ***\n")
# -------------------------------------------------------------------------

実行結果

$ ./text_create.py cities.txt 
*** 開始 ***
cities.txt
*** 終了 ***
cities.txt
t2381	名古屋	52761	2003-4-30
t2382	豊橋	47195	2003-5-10
t2383	岡崎	21674	2003-6-14
t2384	一宮	83912	2003-9-9
t2385	蒲郡	42791	2003-8-4
t2386	常滑	35287	2003-1-21
t2387	大府	81246	2003-7-23
t2388	瀬戸	25791	2003-10-26
t2389	犬山	54139	2003-12-15

ChatGPT が作成したプログラム

cch01.exs
defmodule TextWrite do
  def write_text(file_out, dict_aa) do
    {:ok, fp_out} = File.open(file_out, [:write, :utf8])
    Enum.each(dict_aa, fn {key, unit} ->
      str_out = "#{key}\t#{unit[:name]}\t#{unit[:population]}\t#{unit[:date_mod]}\n"
      IO.write(fp_out, str_out)
    end)
    File.close(fp_out)
  end

  def append_dict(dict_aa, key, name, population, date_mod) do
    dict_aa |> Map.put(key, %{name: name, population: population, date_mod: date_mod})
  end

  def prepare_data() do
    dict_aa = %{} 
    dict_aa =
      dict_aa
      |> append_dict('t2381', '名古屋', 52761, '2003-4-30')
      |> append_dict('t2382', '豊橋', 47195, '2003-5-10')
      |> append_dict('t2383', '岡崎', 21674, '2003-6-14')
      |> append_dict('t2384', '一宮', 83912, '2003-9-9')
      |> append_dict('t2385', '蒲郡', 42791, '2003-8-4')
      |> append_dict('t2386', '常滑', 35287, '2003-1-21')
      |> append_dict('t2387', '大府', 81246, '2003-7-23')
      |> append_dict('t2388', '瀬戸', 25791, '2003-10-26')
      |> append_dict('t2389', '犬山', 54139, '2003-12-15')
    dict_aa
  end
end

# -------------------------------------------------------------------------
IO.stderr |> IO.write("*** 開始 ***\n")

file_out = hd(System.argv)
IO.puts(file_out)

dict_aa = TextWrite.prepare_data()

case TextWrite.write_text(file_out, dict_aa) do
  :ok ->
    :ok
  _error ->
    IO.stderr |> IO.write("*** error *** in text_write_proc ***\n")
end

IO.stderr |> IO.write("*** 終了 ***\n")
# -------------------------------------------------------------------------

実行結果

$ elixir cch01.exs cities.txt 
** (UndefinedFunctionError) function IO.stderr/0 is undefined or private
    (elixir 1.14.0) IO.stderr()
    cch01.exs:33: (file)
    (elixir 1.14.0) lib/code.ex:1245: Code.require_file/2

Gemini が作成したプログラム

cge01.exs
defmodule TextWriter do
  def text_write_proc(file_out, dict_aa) do
    try do
      File.write!(file_out, Enum.map_join(dict_aa, "\n", fn {key, unit} ->
        "#{key}\t#{unit["name"]}\t#{unit["population"]}\t#{unit["date_mod"]}"
      end))
    rescue
      IO.stderr(:stderr, "*** error *** in text_write_proc ***\n")
      raise
    end
  end

  def dict_append_proc(dict_aa, key, name, population, date_mod) do
    Map.put(dict_aa, key, %{"name" => name, "population" => population, "date_mod" => date_mod})
  end

  def data_prepare_proc() do
    dict_aa =
      %{}
      |> dict_append_proc('t2381', '名古屋', 52761, '2003-4-30')
      |> dict_append_proc('t2382', '豊橋', 47195, '2003-5-10')
      # ... (other append calls)
  end
end

IO.stderr(:stderr, "*** 開始 ***\n")

file_out = System.argv |> hd |> hd
IO.puts(file_out)

dict_aa = TextWriter.data_prepare_proc()

try do
  TextWriter.text_write_proc(file_out, dict_aa)
rescue
  IO.stderr(:stderr, "*** error ***\n")
  IO.stderr(:stderr, "#{Exception.message}\n")
end

IO.stderr(:stderr, "*** 終了 ***\n")

実行結果

$ elixir cge01.exs cities.txt 
** (CompileError) cge01.exs:33: expected -> clauses for :rescue in "try"
    (stdlib 4.2) lists.erl:1462: :lists.mapfoldl_1/3
    (stdlib 4.2) lists.erl:1463: :lists.mapfoldl_1/3

私が作成したプログラム

text_create.exs
# ------------------------------------------------------------------
#
#	text_create.exs
#
#						Mar/04/2024
# ------------------------------------------------------------------
defmodule MyModule do
  def dict_display_proc(dict_aa) do
    ll = Map.keys dict_aa
    Enum.each(ll, fn(s) ->
	str_out =  to_string(s) <> "\t" <>  dict_aa[s].name  <> "\t" <> Integer.to_string(dict_aa[s].population) <> "\t" <> dict_aa[s].date_mod
	IO.puts(str_out)
	end)
  end

  def text_write_proc(file_out,dict_aa) do
    {:ok, fp_out} = File.open(file_out, [:write, :utf8])
    ll = Map.keys dict_aa
    Enum.each(ll, fn(s) ->
	str_out =  to_string(s) <> "\t" <>  dict_aa[s].name  <> "\t" <> Integer.to_string(dict_aa[s].population) <> "\t" <> dict_aa[s].date_mod <> "\n"
	IO.write(fp_out,str_out)
	end)
    File.close(fp_out)
  end
end
# ------------------------------------------------------------------
IO.puts :stderr,"*** 開始 ***"
[file_out | _] = System.argv()
IO.puts :stderr,file_out
#
dict_aa = %{
	t2381: %{name: "名古屋", population: 36952, date_mod: "1910-9-27"},
	t2382: %{name: "豊橋", population: 43951, date_mod: "1910-7-21"},
	t2383: %{name: "岡崎", population: 76352, date_mod: "1910-6-12"},
	t2384: %{name: "一宮", population: 28752, date_mod: "1910-1-17"},
	t2385: %{name: "蒲郡", population: 56251, date_mod: "1910-5-9"},
	t2386: %{name: "常滑", population: 16453, date_mod: "1910-9-16"},
	t2387: %{name: "大府", population: 81957, date_mod: "1910-8-5"},
	t2388: %{name: "瀬戸", population: 92947, date_mod: "1910-10-20"},
	t2389: %{name: "犬山", population: 34918, date_mod: "1910-7-11"}
}
#
MyModule.dict_display_proc(dict_aa)
MyModule.text_write_proc(file_out,dict_aa)
IO.puts :stderr,"*** 終了 ***"
# ------------------------------------------------------------------

実行結果

$ elixir text_create.exs cities.txt
*** 開始 ***
cities.txt
t2381	名古屋	36952	1910-9-27
t2382	豊橋	43951	1910-7-21
t2383	岡崎	76352	1910-6-12
t2384	一宮	28752	1910-1-17
t2385	蒲郡	56251	1910-5-9
t2386	常滑	16453	1910-9-16
t2387	大府	81957	1910-8-5
t2388	瀬戸	92947	1910-10-20
t2389	犬山	34918	1910-7-11
*** 終了 ***
cities.txt
t2381	名古屋	36952	1910-9-27
t2382	豊橋	43951	1910-7-21
t2383	岡崎	76352	1910-6-12
t2384	一宮	28752	1910-1-17
t2385	蒲郡	56251	1910-5-9
t2386	常滑	16453	1910-9-16
t2387	大府	81957	1910-8-5
t2388	瀬戸	92947	1910-10-20
t2389	犬山	34918	1910-7-11
2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?