I have a py file like the following one:
final_data = ','.join(ordered_data.iloc[0].astype(str).values.tolist())
runtime = boto3.client('runtime.sagemaker')
response = runtime.invoke_endpoint(EndpointName='xgboost-whatever', #Endpoint mark
ContentType='text/csv',
Body=final_data)
What I want is to replace the line with the EndpointName for another line. Taking in account the SO question Search and replace a line in a file in Python I have coded this:
def replace(file_path, subst):
#Create temp file
fh, abs_path = mkstemp()
with fdopen(fh,'w') as new_file:
with open(file_path) as old_file:
for line in old_file:
if re.search('response = runtime.invoke_endpoint(.*?)#Endpoint mark',line):
new_file.write(line.replace(line, subst))
else:
new_file.write(line)
#Remove original file
remove(file_path)
#Move new file
move(abs_path, file_path)
file_path = '/home/whatever'
subst = " response = runtime.invoke_endpoint(EndpointName='xgboost-otro', #Endpoint mark "
replace(file_path, subst)
However, running the previous code gets me something different that what I was looking for:
final_data = ','.join(ordered_data.iloc[0].astype(str).values.tolist())
runtime = boto3.client('runtime.sagemaker')
response = runtime.invoke_endpoint(EndpointName='xgboost-otro', #Endpoint mark ContentType='text/csv',
Body=final_data)
So I lose the ContentType line, which is converted to a comment.
If I introduce an \n
character at the end of subst
it deletes the ContentType line. How could I solve it?