[テキストデータ を処理する] ためのクラス, Python(パイソン) 使って作ってみた ,ソースプログラムリスト あり
Python(パイソン) プログラム作ってみた インデックス へ
-----
2024.8.28 presented in [note] ( //note.com/runningWater/ )
----------
1 はじめに
これ以降に記述されている内容は、このようなコンピューター・プログラムを制作した、というような事を、ただ、述べているに過ぎない。
以下の記述を読んだ人が、それを単に参考にする、というのであれば、問題は無いと、思われる。
しかし、記述されている内容に沿って、その人が、そこに記されているのと同様の制作や作業を行った際に、その制作、作業、コンピューターの作動の結果、使用されたコンピューター等、様々な方面において、何らかの問題が発生しない、という保証は、全くない。
その制作、作業、コンピューターの作動の結果、その人や、その人が所属している組織、その人が使用した様々な機器、インフラストラクチャー等の、身の上にどのような事が起ころうとも、私は一切、責任を負わない。
このプログラムは、Python(パイソン) 言語を使って、記述されている。
----------
2 どんなものを作ったのか
テキストデータを、ファイルから読み取って、それを処理しやすい形にする
様々なデータを、テキストデータの形式にし、ファイルに書き込む
といったような処理に使えるようなものを、作ってみた。
----------
3 プログラムの内容
下記のような、1個のモジュールを、作ってみた。
コンストラクターの引数に対しては、
arg_abs_path_of_Text_File に、対象とするテキストファイルの絶対パスを、与え
arg_character_used_for_delimiter に、データの間に入れたい文字(例えば、",")を、与え
と、いうようにした。
このようにしてコンストラクトされたインスタンスに対して、オープン、クローズ、リード(読み込み)、ライト(書き込み)など、必要に応じて、用意されているメソッドを呼び出せば、よい、というようにした。
===============
ファイル名 [ TextFileHandling.py ]
----------
#==================================
# This module is only applicable for use with [ encoding , UTF-8 ]
#===========================================
class TextFileHandling :
CV_CLASS_NAME = "TextFileHandling"
#------------------------------------------------------------
# definition of constructer ( set encording to UTF-8 )
def __init__( self
, arg_requester_module
, arg_requester_function
, arg_abs_path_of_Text_File
, arg_character_used_for_delimiter
):
methode_name = "constructer"
print ( "==================================" )
print ( "Enter into , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
self.iv_abs_path_of_Text_File = arg_abs_path_of_Text_File
print ( "self.iv_abs_path_of_Text_File = " + self.iv_abs_path_of_Text_File )
self.iv_character_used_for_delimiter = arg_character_used_for_delimiter
print ( "self.iv_character_used_for_delimiter = " \
+ self.iv_character_used_for_delimiter )
print ( "==================================" )
self.iv_encoding = "UTF-8"
print ( "self.iv_encoding = " + self.iv_encoding )
self.iv_Opened = " "
self.iv_current_order_number = 0
self.iv_list_made_by_reading = ""
print ( "==================================" )
print ( "Exit from , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
#------------------------------------------------------------
def do_open_process_input_mode ( self
, arg_requester_module
, arg_requester_function ) :
methode_name = "do_open_process_input_mode"
print ( "==================================" )
print ( "Enter into , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
try :
self.iv_Opened \
= open ( self.iv_abs_path_of_Text_File \
, encoding = self.iv_encoding \
)
except IOError as error_return :
print ( "==================================" )
print ( "Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==== Open Error !!! ======================" )
print ( error_return )
return "E"
print ( "==================================" )
print ( "Exit from , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "Text file has been opened" )
print ( "self.iv_abs_path_of_Text_File = " \
+ self.iv_abs_path_of_Text_File )
print ( "==================================" )
return "Y"
#------------------------------------------------------------
def do_open_process_output_mode ( self
, arg_requester_module
, arg_requester_function ) :
methode_name = "do_open_process_input_mode"
print ( "==================================" )
print ( "Enter into , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
output_mode = "w"
try :
self.iv_Opened \
= open ( self.iv_abs_path_of_Text_File \
, output_mode \
, encoding = self.iv_encoding \
)
except IOError as error_return :
print ( "==================================" )
print ( "Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==== Open Error !!! ======================" )
print ( error_return )
return "E"
print ( "==================================" )
print ( "Exit from , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "Text file has been opened" )
print ( "self.iv_abs_path_of_Text_File = " \
+ self.iv_abs_path_of_Text_File )
print ( "==================================" )
return "Y"
#------------------------------------------------------------
def do_close_process ( self
, arg_requester_module
, arg_requester_function ) :
methode_name = "do_close_process"
print ( "==================================" )
print ( "Enter into , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( " arg_requester_module = " + arg_requester_module
+ " , arg_requester_function = " + arg_requester_function )
print ( "==================================" )
self.iv_Opened \
.close ( )
print ( "==================================" )
print ( "Exit from , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "Text file has been closed" )
print ( "self.iv_abs_path_of_Text_File = " \
+ self.iv_abs_path_of_Text_File )
print ( "==================================" )
#------------------------------------------------------------
def do_read_one_line_process ( self
, arg_requester_module
, arg_requester_function
) :
methode_name = "do_read_one_line_process"
#print ( "==================================" )
#print ( "Enter into , Class = " + TextFileHandling.CV_CLASS_NAME
# + " , methode = " + methode_name )
#print ( " arg_requester_module = " + arg_requester_module
# + " , arg_requester_function = " + arg_requester_function )
#print ( "==================================" )
return_list = [ ]
contents_of_one_line = \
self.iv_Opened \
.readline ( )
#print ( "contents_of_one_line = " + contents_of_one_line )
if ( contents_of_one_line == "" ) :
# ------ reached to file end ------
print ( "==================================" )
print ( "Exit from , Class = " + TextFileHandling.CV_CLASS_NAME
+ " , methode = " + methode_name )
print ( "reached to file end" )
print ( "==================================" )
return_list \
.append ( "E" )
return return_list
# --------- one line data readed ---------------
return_list \
.append ( "R" )
#remove "\n"
w_removed = \
contents_of_one_line \
.removesuffix ( "\n" )
#print ( "w_removed = " + w_removed )
splited_one_line = [ ]
splited_one_line = w_removed \
.split ( \
self.iv_character_used_for_delimiter \
)
#print ( "splited_one_line = " )
#print ( splited_one_line )
for one_value in splited_one_line :
return_list \
.append ( one_value )
#print ( "==================================" )
#print ( "Exit from , Class = " + TextFileHandling.CV_CLASS_NAME
# + " , methode = " + methode_name )
#print ( "one line have been readed" )
#print ( "return_list = " )
#print ( return_list )
#print ( "==================================" )
self.iv_list_made_by_reading = return_list
return return_list
#------------------------------------------------------------
def do_write_one_line_process ( self
, arg_requester_module
, arg_requester_function
, arg_list_values_for_output_one_line
) :
methode_name = "do_write_one_line_process"
#print ( "==================================" )
#print ( "Enter into , Class = " + TextFileHandling.CV_CLASS_NAME
# + " , methode = " + methode_name )
#print ( " arg_requester_module = " + arg_requester_module
# + " , arg_requester_function = " + arg_requester_function )
#print ( "==================================" )
size_of_list_values_for_output_one_line = \
len ( arg_list_values_for_output_one_line )
output_line_string = ""
# --------------------------------------
for index_of_list_values \
in range ( size_of_list_values_for_output_one_line ) :
item_value = arg_list_values_for_output_one_line \
[ index_of_list_values ]
# --------------------
w1 = output_line_string
if ( index_of_list_values \
<= ( size_of_list_values_for_output_one_line - 2 ) ) :
output_line_string = w1 + str ( item_value ) \
+ self.iv_character_used_for_delimiter
else :
# process for last item value in list
output_line_string = w1 + str ( item_value ) \
+ "\n"
# --------------------------------------
self.iv_Opened \
.write ( output_line_string )
----------
Python(パイソン) プログラム作ってみた インデックス へ