To my understanding, Python will escape escaped characters when reading values from a file. For example, the newline character \n
will become \\n
.
However, when I am writing into an output file I want a newline instead of printing \n
.
Code example:
output_file = open(os.path.join(path, file), 'a')
value1 = "abcd"
value2 = "1234"
delimiter = "\\n" # in reality not hardcoded
output_file.write(value1 + delimiter + value2)
output_file.close()
I want this to show up as:
abcd
1234
While it's currently showing as:
abcd\n1234
While it's possible to do a replace()
, I want to avoid this because delimiter may be ANY escaped value. (ie. I don't want the following):
.replace('\\n', '\n').replace('\\t', '\t')...
Edit:
The file reading example is as follows:
try:
with open(input_data, 'r') as input:
for line in input:
...
key,value = line.split('=', 1)
...
if (key.strip() == 'delimiter'): # in reality done through loop
index_dict['delimiter'] = value
and the file contains:
delimiter="\n"