with open(filename, 'r') as text_file:
    contents = text_file.read().rstrip()
with open(filename, 'w') as text_file:
    text_file.write(contents)
Writing a file in python in a key=value1,....valuen format