The str()conversion converts "\ n" to "\\ n".
>>> str('\n')
'\n'
>>> str(['\n'])
"['\\n']"
What is happening there? When you call str()in a list (the same for a tuple), it will call a __str__()list method , which in turn calls __repr__()for each of its elements. Check out what his behavior is:
>>> "\n".__str__()
'\n'
>>> "\n".__repr__()
"'\\n'"
So you have a reason.
As for how to fix this, as Blender suggested, the best option would be to not use str()the list:
''.join(str(x) for x in itemsToWriteToFile)
source
share