I have a text file like this
i am a messi fan \t
I am using this code to read
f = open(input_para['controle_file'], 'r')
filedata = f.read().splitlines()
now my output is
i am a messi fan \\t
required output is i am a messi fan \t
How can I get this exact data from the text file in python?
The difference is based on how python represents a string. If you print it, you should see what you want.
>>> r"foo\t"
'foo\\t'
>>> print r"foo\t"
foo\t
This boils down to the difference between repr and str ...
>>> s = r"foo\t"
>>> print str(s)
foo\t
>>> print repr(s)
'foo\\t'
And the reasoning is that if possible, repr should return a string suitable for recreating the object. In your case, if repr didn't add an extra escaping backslash, then the string representation would look like it had a tab character in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With