So I am trying to write a few different values of this list to a few specific lines of this text file essentially replacing them. I got a tip on my last question to seperate the lines in this output I need to do \n however I am struggling to implement this within this list expression. If I do it within the writeline command it gives me every line within the file with an extra whitespace line. If anyone has any ideas please let me know. this is the code:
def writeAPconstants(roll_pred , roll_resp, roll_error, roll_rate, roll_tunetime):
# takes line index to map function inputs as new autopilot constant values
APlist[line_index[0]] = "P acf/_ott_phi_sec_into_future" ' ' roll_pred
APlist[line_index[1]] = "P acf/_ott_roll_response" ' ' roll_resp
APlist[line_index[2]] = "P acf/_ott_phi_deg_off_for_full_def" ' ' roll_error
APlist[line_index[3]] = "P acf/_ott_roll_rate" ' ' roll_rate
APlist[line_index[4]] = "P acf/_ott_phi_sec_to_tune" ' ' roll_tunetime
APfile.seek(0)
APfile.writelines(APlist)
APfile.truncate()
#closes file
APfile.close()
CodePudding user response:
The lines returned by readlines()
have newlines appended. You need to add newlines to the end of each string you assign to a list member.
APlist[line_index[0]] = "P acf/_ott_phi_sec_into_future" ' ' roll_pred '\n'
APlist[line_index[1]] = "P acf/_ott_roll_response" ' ' roll_resp '\n'
APlist[line_index[2]] = "P acf/_ott_phi_deg_off_for_full_def" ' ' roll_error '\n'
APlist[line_index[3]] = "P acf/_ott_roll_rate" ' ' roll_rate '\n'
APlist[line_index[4]] = "P acf/_ott_phi_sec_to_tune" ' ' roll_tunetime '\n'