大容量繰り返しテキストデータからのエクセル用データ抽出Pythonスクリプト ― 2025年02月21日 20:32
複数行からなる繰り返しのあるテキストデータから指定したWORDを含む任意の行データを抽出し、連結して1行のテキストデータとするためのPythonプログラムである。
この程度のプログラムはChatGPTに頼むと1分程度で作成してくれる。実際に使ってみて不具合があれば、プログラムとエラー内容を再度ChatGPTにそのまま入力することで修正プログラムを提示してくれる。これは無料版のChatGPTである。
ChatGPTにログインアカウントを作れば、依頼した簡単なプログラムを記憶、記録してくれるので、再度ログインしたときにその記録(AIが要約した文として表示される)をクリックすることで、以前の記録作業のもとに新たな改良プログラムを作成することも可能である。
このPythonスクリプトで対象となるデータはあるプログラムの出力データであり、繰り返し計算した結果を出力する。この出力データファイルの中にある任意の文字を含む行を連結し、このPythonスクリプトは連結された1行のテキストデータとして出力する。
この1行のテキストデータは同じ長さを持っており、エクセルで読み込んで区切りをつけることで表データに変換し、元データの分析することを目的としている。
元データはFile.txtでこのpythonスクリプトと同じフォルダーにあることを前提としている。
検索文字はword1~word4の4文字列としている。File.txtはこれらの文字列が繰り返し現れるtextファイルであることを前提としている。
データ抽出Pythonスクリプト
def extract_lines_with_words(file_path, target_word, next_word, next2_word, next3_word):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
result = []
all_lines = [] # 各ステップの行を格納するリスト
line1 = None # target_word を含む行
line2 = None # next_word を含む行
line3 = None # next2_word を含む行
line4 = None # next3_word を含む行
for line in lines:
line = line.strip() # 前後の空白や改行を削除
if target_word in line:
line1 = line # target_word を含む行を保存
line2 = None # リセット
elif next_word in line and line1:
line2 = line # 次の単語を含む行(連結しない)
line3 = None # 状態リセット
elif next2_word in line and line2:
line3 = line # 3つ目の単語を含む行(連結しない)
line4 = None # 状態リセット
elif next3_word in line and line3:
line4 = line # 4つ目の単語を含む行(連結しない)
all_lines.append((line1, line2, line3, line4)) # 各ステップをリストに保存
# 文字数の最大値を求める(空のデータがあってもエラーにならないようにする)
max_len1 = max((len(l[0]) for l in all_lines if l[0]), default=0)
max_len2 = max((len(l[1]) for l in all_lines if l[1]), default=0)
max_len3 = max((len(l[2]) for l in all_lines if l[2]), default=0)
max_len4 = max((len(l[3]) for l in all_lines if l[3]), default=0)
# 各行を最大文字数に揃えて空白区切りでフォーマット
formatted_results = []
for l1, l2, l3, l4 in all_lines:
l1 = l1.ljust(max_len1) if l1 else " " * max_len1
l2 = l2.ljust(max_len2) if l2 else " " * max_len2
l3 = l3.ljust(max_len3) if l3 else " " * max_len3
l4 = l4.ljust(max_len4) if l4 else " " * max_len4
formatted_results.append(f"{l1} {l2} {l3} {l4}") # タブの代わりにスペースを使用
return formatted_results
# ファイルとキーワードを指定
file_path = "File.txt"
target_word = "word1"
next_word = "word2"
next2_word = "word3:"
next3_word = "word4:"
# 関数を実行して結果を取得
extracted_lines = extract_lines_with_words(file_path, target_word, next_word, next2_word, next3_word)
# 結果を表示
print(file_path)
for line in extracted_lines:
print(line)
この程度のプログラムはChatGPTに頼むと1分程度で作成してくれる。実際に使ってみて不具合があれば、プログラムとエラー内容を再度ChatGPTにそのまま入力することで修正プログラムを提示してくれる。これは無料版のChatGPTである。
ChatGPTにログインアカウントを作れば、依頼した簡単なプログラムを記憶、記録してくれるので、再度ログインしたときにその記録(AIが要約した文として表示される)をクリックすることで、以前の記録作業のもとに新たな改良プログラムを作成することも可能である。
このPythonスクリプトで対象となるデータはあるプログラムの出力データであり、繰り返し計算した結果を出力する。この出力データファイルの中にある任意の文字を含む行を連結し、このPythonスクリプトは連結された1行のテキストデータとして出力する。
この1行のテキストデータは同じ長さを持っており、エクセルで読み込んで区切りをつけることで表データに変換し、元データの分析することを目的としている。
元データはFile.txtでこのpythonスクリプトと同じフォルダーにあることを前提としている。
検索文字はword1~word4の4文字列としている。File.txtはこれらの文字列が繰り返し現れるtextファイルであることを前提としている。
データ抽出Pythonスクリプト
def extract_lines_with_words(file_path, target_word, next_word, next2_word, next3_word):
with open(file_path, 'r', encoding='utf-8') as file:
lines = file.readlines()
result = []
all_lines = [] # 各ステップの行を格納するリスト
line1 = None # target_word を含む行
line2 = None # next_word を含む行
line3 = None # next2_word を含む行
line4 = None # next3_word を含む行
for line in lines:
line = line.strip() # 前後の空白や改行を削除
if target_word in line:
line1 = line # target_word を含む行を保存
line2 = None # リセット
elif next_word in line and line1:
line2 = line # 次の単語を含む行(連結しない)
line3 = None # 状態リセット
elif next2_word in line and line2:
line3 = line # 3つ目の単語を含む行(連結しない)
line4 = None # 状態リセット
elif next3_word in line and line3:
line4 = line # 4つ目の単語を含む行(連結しない)
all_lines.append((line1, line2, line3, line4)) # 各ステップをリストに保存
# 文字数の最大値を求める(空のデータがあってもエラーにならないようにする)
max_len1 = max((len(l[0]) for l in all_lines if l[0]), default=0)
max_len2 = max((len(l[1]) for l in all_lines if l[1]), default=0)
max_len3 = max((len(l[2]) for l in all_lines if l[2]), default=0)
max_len4 = max((len(l[3]) for l in all_lines if l[3]), default=0)
# 各行を最大文字数に揃えて空白区切りでフォーマット
formatted_results = []
for l1, l2, l3, l4 in all_lines:
l1 = l1.ljust(max_len1) if l1 else " " * max_len1
l2 = l2.ljust(max_len2) if l2 else " " * max_len2
l3 = l3.ljust(max_len3) if l3 else " " * max_len3
l4 = l4.ljust(max_len4) if l4 else " " * max_len4
formatted_results.append(f"{l1} {l2} {l3} {l4}") # タブの代わりにスペースを使用
return formatted_results
# ファイルとキーワードを指定
file_path = "File.txt"
target_word = "word1"
next_word = "word2"
next2_word = "word3:"
next3_word = "word4:"
# 関数を実行して結果を取得
extracted_lines = extract_lines_with_words(file_path, target_word, next_word, next2_word, next3_word)
# 結果を表示
print(file_path)
for line in extracted_lines:
print(line)
コメント
トラックバック
このエントリのトラックバックURL: http://yokoyamashindo.asablo.jp/blog/2025/02/21/9756298/tb
※なお、送られたトラックバックはブログの管理者が確認するまで公開されません。
コメントをどうぞ
※メールアドレスとURLの入力は必須ではありません。 入力されたメールアドレスは記事に反映されず、ブログの管理者のみが参照できます。
※なお、送られたコメントはブログの管理者が確認するまで公開されません。