Created
July 12, 2024 13:13
-
-
Save tohka/e071bda645b3db93521cd3f29290bfec to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
""" | |
1,-42000.75,108723.75,132.60,0 | |
2,-42000.25,108723.75,132.30,0 | |
3,-42001.75,108723.25,132.80,0 | |
: | |
: | |
という形式の CSV ファイルを | |
-42000.750 108723.750 132.600 | |
-42000.250 108723.750 132.300 | |
-42001.750 108723.250 132.800 | |
: | |
: | |
という空白区切りの形式に変換するスクリプト | |
""" | |
import glob | |
import re | |
# 動作させているフォルダ内で、拡張子が csv のファイルのみ抽出 | |
# (正確には全ファイル抽出したのち、 csv 以外を除外) | |
csv_pattern = re.compile(r'\.csv$', re.IGNORECASE) | |
for csv_file in glob.glob(r'*'): | |
if not re.search(csv_pattern, csv_file): | |
continue | |
# 変換したデータは拡張子 txt で保存 | |
# ここでは簡易処理のため、既に同名ファイルがあっても上書き | |
txt_file = re.sub(csv_pattern, r'.txt', csv_file) | |
with open(txt_file, 'w') as f_out: | |
print(f"convert {csv_file} to {txt_file} ...") | |
# csv ファイルを開く | |
with open(csv_file) as f_in: | |
# csv ファイルから1行ずつ取得して処理 | |
for line in f_in: | |
# 入力行からカンマで分割し2, 3, 4列目(座標値)のみ抽出 | |
_, x, y, z, _ = line.strip().split(r',') | |
# 各座標値の書式を小数点以下3桁にし、連結 | |
l = r' '.join(map(lambda v: f"{float(v):.3f}", [x, y, z])) | |
# txt ファイルに出力 | |
f_out.write(f"{l}\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment