2

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?

Javier Lopez Tomas
  • 2,072
  • 3
  • 19
  • 41
  • `line.replace(line, subst)` is so cryptic way of saying `subst`. But you need to add a newline. Use `new_file.write("{}\n".format(subst))` instead of `new_file.write(line.replace(line, subst))` – Wiktor Stribiżew Oct 02 '19 at 10:03
  • Adding the 'r' has solved the problem. Thanks! Make an answer so that I can accept it (if you want, of course) – Javier Lopez Tomas Oct 02 '19 at 10:13

2 Answers2

1

You forgot the \n at the end of the line:

subst = "        response = runtime.invoke_endpoint(EndpointName='xgboost-otro', #Endpoint mark\n"

and in your replacing logic:

   new_file.write(subst)
Charif DZ
  • 14,415
  • 3
  • 21
  • 40
1

You need to open the file in read mode:

with open(file_path, 'r') as old_file:

Also, line.replace(line, subst) is so cryptic way of saying subst. You need to add a newline at the end of subst anyway or use

new_file.write("{}\n".format(subst)) 

instead of

new_file.write(line.replace(line, subst))
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563