【发布时间】:2022-01-09 11:43:19
【问题描述】:
我有一个名为experiments.txt 的文件,其中包含一个名为script.py 的python 脚本的参数。
../data/20211015_08-09-50CST_raw_fold_results-mlr.csv --plot True
../data/20211115_08-15-35CST_raw_fold_results-rf.csv --plot True
假设文件夹结构和文件如下图所示,注意data/中的csv文件与experiments.txt中的文件不相同。
data
|___20211117_09-10-50CST_raw_fold_results-mlr.csv
|___20211117_09-11-35CST_raw_fold_results-rf.csv
src
|___script.py
|___experiments.txt
我想替换第一个参数
(例如,../data/20211015_08-09-50CST_raw_fold_results-mlr.csv)
对于experiments.txt 中的每一行,使用更新后的数据使experiments.txt(或创建一个新文件,如experiments-2.txt)变为
..\data\20211117_09-10-50CST_raw_fold_results-mlr.csv --plot True
..\data\20211117_09-11-35CST_raw_fold_results-rf.csv --plot True
我知道我可以使用 Python 编写一个笨拙的解决方案,但我的解决方案充其量似乎不是最理想的,最坏的情况是设计非常糟糕。 如何在 bash 中执行所需的任务(因为它似乎很适合该任务,但我不确定如何)?
# This sample solution is written in `.ipynb` in the `src/` directory
import os
from pathlib import Path
cwd = os.getcwd() # src
replacement_fnames = [file for file in os.listdir(os.path.join(cwd, '..', 'data'))]
with open('experiments.txt', 'r') as fobj:
lines = [line.strip() for line in fobj.readlines()]
# The replacement lines for the file `experiments-2.txt` will be
# appended to this empty string
write_str = ''
for line in lines:
# A line in the file is of the form
# `path <SPACE> opts`, therefore splitting the line into a
# list delimited by a space `' '` allows access to the `path`
# by indexing 0
space_separated_line = line.split(' ')
cur_path = Path(space_separated_line[0])
cur_fname = Path(cur_path).name
# File names are separated by model name... in this case
# `mlr` and `rf`... by splitting the file name into a list
# delimited by `-`, then the last element of that list is the
# name of the model
# e.g., cur_fname = 20211015_08-09-50CST_raw_fold_results-mlr.csv
# cur_fname.split('-') --> ['20211015_08-09-50CST_raw_fold_results', 'mlr.csv']
cur_fname_model_name = cur_fname.split('-')[-1]
for replacement_fname in replacement_fnames:
# Extract model name from the replacement fname in the same
# fashion as done for cur_fname
replacement_fname_model_name = replacement_fname.split('-')[-1]
if replacement_fname_model_name == cur_fname_model_name:
space_separated_line[0] = os.path.join(Path(cur_path).parent, replacement_fname)
write_str += ' '.join(space_separated_line) + '\n'
print('Original:')
print('\n'.join(lines))
print()
print('Replaced:')
print(write_str)
with open('experiments-2.txt', 'w') as fobj:
fobj.write(write_str)
## Output
# Original:
# ../data/20211015_08-09-50CST_raw_fold_results-mlr.csv --plot True
# ../data/20211115_08-15-35CST_raw_fold_results-rf.csv --plot True
# Replaced:
# ..\data\20211117_09-10-50CST_raw_fold_results-mlr.csv --plot True
# ..\data\20211117_09-11-35CST_raw_fold_results-rf.csv --plot True
【问题讨论】: