How to fix line break when concatenating string with variable in python?

When iterating over the file contents sample_text contains newline characters at the end, e.g. '$.SyncCustomerRequestABM.SyncCustomerRequest.Customers.Customer.OriginalSystemReference\n'

The issue is that the nodeid by splitting the line and taking the last element of the split.

You can fix the problem by stripping the sample_text at the beginning of each iteration:

sample_text = sample_text.strip()

The reason why it worked for the last line is that it does not contain the newline character in your file.


This will help you with your existing code but I also strongly suggest looking into better ways of generating these string:

  • database libraries allow you pass parameters to SQL queries e.g. psycopg https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries
  • using f-strings to avoid boilerplate code https://realpython.com/python-f-strings/