After connecting to the server, like in the demo, this is how I would edit the remote file:

with sftp.open("yourfiletoedit", "rb") as f:
data = f.read()
# modify the data of the file (this depends on the format wich was not specified)
with sftp.open("yourfiletoedit", "wb") as f:
f.write(data)

Email codedump link for python solution to ssh into a linux server and edit a file