0

I am trying to write a code that does some data processing on some files. I have written logic to split my files into paragraphs. currently, I am splitting using '\n\r'. I want to read the splitting condition in a file so that I can easily change them in the future if required. However, the '\n\r' I read from the file is not behaving the way I want it to behave

I wrote \n\r in a file and when I read it is different from a variable being assigned '\n\r'

>>> vr = '\n\r'
>>> from_file = open('test.txt').read()
>>> print (vr)


>>> print (from_file)
\n\r
>>> print (len(vr))
2
>>> print (len(from_file))
4

So how can I make what I convert what I read from the file to normal python string?

Thanks

George
  • 109
  • 8
  • Did you actually write "\" followed by "n" followed by "\" followed by "r" - or "\n\r"? (It is not the same.) The former is four characters, the latter is two characters. – DYZ May 02 '19 at 04:55

1 Answers1

1

What you want to do is to parse the string read in, and convert special sequences of characters to characters that would be otherwise difficult to type.

Parsing, writ large, can be a very complicated topic. If all you want to do is to replace the backslash escapes, you can do a simple string replacement:

from_file = from_file.replace("\\r", "\r")
from_file = from_file.replace("\\n", "\n")
from_file = from_file.replace("\\t", "\t")
...
from_file = from_file.replace("\\\\", "\") # always last

This is very very naive but meets your requirements as stated.

PaulProgrammer
  • 16,175
  • 4
  • 39
  • 56