2

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"
Kevin Y
  • 45
  • 1
  • 8

2 Answers2

1

Python isn't escaping anything coming from your data file. The backslash character (and the quotation marks) are actually in the file text. It sounds like you want to evaluate the string after you read it, so that rather than value being '"\\n"' (a string with double quotes around a backslash and an "n" character), you get '\n'(a string with a single newline character).

I suggest using ast.literal_eval. It works similar to the builtin eval function, but won't ever call functions or do other dangerous stuff, while eval might. In addition to strings it will also evaluate other kinds of Python literals (e.g. int, float and complex numbers, and containers like list, tuple, set and dict (as long as the contents of the containers are also literals).

if (key.strip() == 'delimiter'):
    index_dict['delimiter'] = ast.literal_eval(value)
Blckknght
  • 100,903
  • 11
  • 120
  • 169
  • Thank you! This is a perfect answer. Not only did you address the root of the problem but you also expanded my understanding of the Python language. Up until now, I've only been using ast.literal_eval to expand flattened dictionaries. Much appreciated. – Kevin Y Jun 18 '15 at 17:17
0

Change your delimiter variable from "\\n" to "\n", that should fix it.

EDIT: This question will help you solve your problem, look at the accepted answer for an example.

Process escape sequences in a string in Python

Community
  • 1
  • 1
Ogen
  • 6,499
  • 7
  • 58
  • 124
  • When you get it, just say `if delimiter == '\\n': delimiter = '\n'`. easy. – Ogen Jun 18 '15 at 01:03
  • It's not only newline. If I was only targeting newline, a simple .replace('\\n', '\n') would fix it. – Kevin Y Jun 18 '15 at 01:07
  • You'll be better off finding out why your data is coming in with double blackslashes and rectify that instead. This is a huge problem because single backslashes are not valid literals so you can't really manipulate them... – Ogen Jun 18 '15 at 01:19