LoginSignup
0
0

理解しました。以下は、指定された処理をPythonで実行するコードです。空白を無視して指定された文字列を抜き出し、CSVに書き出します。

import csv

# ASCファイルのパス
asc_file_path = "path/to/your/file.asc"

# CSVファイルのパス
csv_file_path = "path/to/your/output/file.csv"

# データ抽出とCSV書き出しの関数
def process_asc_and_export_to_csv(asc_path, csv_path):
    with open(asc_path, 'r') as asc_file, open(csv_path, 'w', newline='') as csv_file:
        csv_writer = csv.writer(csv_file)
        csv_writer.writerow(["ExtractedData"])  # CSVヘッダー

        for line in asc_file:
            if line.strip().startswith(("0 0 8  8 10 12 67 01", "0 0 8  8 21", "0 0 8  8 22")):
                # 各行から指定された位置の文字列を抜き出す(空白を無視)
                data_1 = line.split("10 12 67 01", 1)[1][:4].strip() if "10 12 67 01" in line else ""
                data_2 = line.split("21", 1)[1][:7].strip() if "21" in line else ""
                data_3 = line.split("22", 1)[1][:5].strip() if "22" in line else ""

                # 1〜3の内容を連結してCSVに書き込む
                concatenated_data = f"{data_1}{data_2}{data_3}"
                csv_writer.writerow([concatenated_data])

# 処理を実行
process_asc_and_export_to_csv(asc_file_path, csv_file_path)

このコードは、指定された文字列が各行の先頭にあるかどうかを確認し、それに基づいて各行から指定された位置の文字列を抜き出しています。CSVには1行ずつ書き込んでいます。

0
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
0
0