我正在尝试将转换后的etl表保存到特定目录中的CSV文件中。我有以下方法:
def save_transformed_data_to_csv(self, data, path):
fund = data.values('FundCode')[0]
# 'convert' transforms date strings into datetime objects
start_date = convert(data.values('PositionStartDate')[0], date)
end_date = convert(data.values('PositionDate')[0], date)
filename = 'citco_transformed_positions_{fund}_{start}_{end}.csv'.format(fund=fund, start=start_date, end=end_date)
filepath = Path(path, filename)
data.tocsv(filepath)
打印出格式化的文件名将返回以下内容:
citco_transformed_positions_>PWPR1_2018-07-01_2018-07-11.csv
我这样称呼它:
# Store transformed data for unmapping processing
loader.save_transformed_data_to_csv(transformed_data, '/Users/cgrijalva/Misc/UnmappedPositions')
运行它时,出现以下错误:
OSError: [Errno 22] Invalid argument: '\\Users\\cgrijalva\\Misc\\UnmappedPositions\\citco_transformed_positions_>PWPR1_2018-07-01_2018-07-11.csv'
答案 0 :(得分:0)
正如Rakesh所指出的,文件名或路径中不能包含>
。一种解决方法是:
import re
fname = 'citco_transformed_positions_{fund}_{start}_{end}.csv'.format(fund=fund, start=start_date, end=end_date)
fname = re.sub('[^\w\-_\. ]', '_', fname)
print(fname)
citco_transformed_positions__PWPR1_2018-07-01_2018-07-11.csv