read escaped character \t as '\t' instead of '\\t' Python ConfigParser [duplicate] - python

This question already has answers here:
Process escape sequences in a string in Python
(8 answers)
Closed 7 months ago.
I have a config.ini file containing delimiter = \t which i now want to read using the python3 ConfigParser.
However, the resulting string is '\\t' instead of '\t' which breaks my program.
Is there a more elegant option to solve this problem instead of just manually stripping the extra '\' from every variable containing an escaped character?
I cannot find an option for ConfigParser().read() to not escape the backslash it finds in the file.

Python3 has a 'unicode_escape' codec.
r"a\tb".decode('unicode_escape')
'a\tb'
Sources:
https://bytes.com/topic/python/answers/37952-escape-chars-string
how do I .decode('string-escape') in Python3?

Related

Red text after "/". Make unreadable [duplicate]

This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 15 days ago.
enter image description here
i use Python and Visual Studio Code IDE
Is there a way to fix this?
Backslashes in strings are ”escape characters” used to signify special characters such as newline (\n), tab (\t), and so on.
To put an actual backslash in a string, use a double backslash: this\\that, or use an r-string: r”this\that”.

How do you print \ in python [duplicate]

This question already has answers here:
How can I print a single backslash?
(4 answers)
Closed 2 years ago.
I would like to print a backslash character:
\
But whne I try to use escape characters
print(\"\"\)
I get a syntax error. I have looked into ecape characters, however is their a specific one for backslash ()?
If you do
print("\")
you are escaping the final quote.
You need to escape the backslash.
print("\\")
It is simple, just escape the escape character itself:
print("\\")

How would I write a newline symbol to a file? [duplicate]

This question already has answers here:
In Python, is it possible to escape newline characters when printing a string?
(3 answers)
Closed 5 years ago.
I'm trying to write code to a python file from a python program
Example:
f.write("output.write(str(fib(int(line))) + '\n')")
I want the newline symbol to actually be written to the file and not place an actual newline in the file. How would I go about doing this?
You just need to escape the backslash with another backslash:
... + '\\n'
Or use raw strings:
... + r'\n'
try adding another backslash \ before the newline symbol like so:
f.write("output.write(str(fib(int(line))) + '\\n')")

\r or \n added in arguments list from Python [duplicate]

This question already has answers here:
How can I remove a trailing newline?
(27 answers)
Closed 6 years ago.
i have some scripts that take as input one or multiple paths as on argument.
The script is run like that: myScript.py D:\Folder1,E:\OtherData\Files
In the script, i split the path arguments in the comma and i read the paths.
The problem is that Python adds a \r in the end of each path for no reason.
So the script tries to read D:\Folder1\r and E:\OtherData\Files\r.
Why is this and how can i solve it?
There are many ways, here is one for example.
Replacing the relevant problematic string with an empty string:
paths= [x.rstrip() for x in paths_list]
The method rstrip() returns a copy of the string in which all chars have been stripped from the end of the string (default whitespace characters).

Python read file - pass variable name [duplicate]

This question already has answers here:
What exactly do "u" and "r" string prefixes do, and what are raw string literals?
(7 answers)
Closed 7 years ago.
Very simple question for Python 2:
I am calling specific library/function passing filename with readonly flag:
myfunction(r'/tmp/file.txt')
I wanted to replace it with variable:
filename = '/tmp/file.txt'
myfunction(r????)
How can I call that function?
That is not readonly flag. That means raw string. You use it when you don't want escape sequences inside string to be interpreted (like \n, \t etc.) See https://docs.python.org/2.0/ref/strings.html
For your string, you don't need it since it does not contain any escape sequence. Just omit the leading r.
filename = r'/tmp/file.txt'
myfunction(filename)
in most cases you can use
myfunction(r''+filename)
if you don't want to define the r prefix in your variable. This works with b'' and u'' too.

Categories