python replace \\ with \ in stringpath automatically - python-3.x

how can I replace "\" in path string with "\\" python, u know \ is for escape character and r'\' and r"\" also don't work, neither in str.replace() or in re.sub()

If your objective is to get the correct path you can use the raw string:
r"C:\Users"
# will return
Out[2]: 'C:\\Users'
# in the console
#however if you print it, it will print this:
print(r"C:\Users")
C:\Users
if you want to combine parts of the path dynamically i recommend the os library (standard library)
use it like this:
import os
path = os.path.join(r"first_part_of_path", r"other_part_of_path", "filename.xlsx")

from python's documentation: "The solution is to use Python’s raw string notation for regular expression patterns; backslashes are not handled in any special way in a string literal prefixed with 'r'. So r"\n" is a two-character string containing '' and 'n', while "\n" is a one-character string containing a newline. Usually patterns will be expressed in Python code using this raw string notation."
https://docs.python.org/3/library/re.html
below maybe what you are looking for:
x=r'this, is a \test'
re.subn('\\','\\',x)

from the standard library, you could use os.path.normpath
Example:
import os
myDir = r"path\to\dir"
normalized = os.path.normpath(myDir)
Which enables the following :
>>> normalized
'path\\to\\dir'
>>> print(normalized)
path\to\dir
>>> str(normalized)
'path\\to\\dir'
>>> repr(normalized)
"'path\\\\to\\\\dir'"

I just realized our path for i.e.
path_str="E:\neural network\Pytorch"
can be changed to
path_str=path_str.encode('unicode-escape').decode().replace('\\\\', '\\')
and this would also do it automatically without need to manipulating the string manually to
path_str=r"E:\neural network\Pytorch"

Related

Using "r" for escape sequence when file in Julia

On the Python using r front of the file path, can deal with escape sequence such as :
df = pd.read_csv(r"D:\datasets\42133.csv")
However on Julia, the below code returns, MethodError: no method matching joinpath(::Regex)
file_path = r"D:\datasets\42133.csv"
df = DataFrame(CSV.File(file_path))
I checked this, and know that I can chage \ to \\ or /. But wondering that why Julia does not allowed to use r"String"? Also is there something like r"String" on Julia?
You are looking for raw"..." string.
julia> raw"D:\datasets\42133.csv"
"D:\\datasets\\42133.csv"
In Julia, r"..." strings create a Regex object.

Jinja2 NativeTemplate rendering strings with double quotes issue

I'm reading a path value from Postgres DB (column type String).
For example:
path | "G:\Shared drives\2 test\2021\08.2021\test.xlsx"
The problem is that some of the nested directories in the path starts with integer (as the above), and Python automatically treats those as Hex characters.
\2 is converted to \x02.
\08 (treated as \0) converted to \x008
\2021 (treated as \20) converted to \x821
print(repr('"G:\Shared drives\2 test\2021\08.2021\test.xlsx"'))
> '"G:\\Shared drives\x02 test\x821\x008.2021\test.xlsx"'
How can I stop Python from interpreting these hex values, and treat it as raw string?
Expected result:
'"G:\\Shared drives\\2 test\\2021\\08.2021\test.xlsx"'
Edit:
It seems that the value is correct on the DB side, and also when I read it.
The path is getting corrupted once I render it with Jinja2 NativeTemplate (specifically native template).
import jinja2
env = jinja2.nativetypes.NativeEnvironment()
path = '"G:\\Shared drives\\2 test\\2021\\08.2021\\test.xlsx"'
print(path)
> "G:\Shared drives\2 test\2021\08.2021\test.xlsx"
t = env.from_string('{{ path }}')
result = t.render(path=path)
print(result) # result is broken
> G:\Shared drives test18.2021 est.xlsx
print(repr(result))
> 'G:\\Shared drives\x02 test\x821\x008.2021\test.xlsx'
If I remove the double quotes from the path string, the render output is valid:
path = 'G:\\Shared drives\\2 test\\2021\\08.2021\\test.xlsx'
result = t.render(path=path)
print(result) # works without double quotes
> G:\Shared drives\2 test\2021\08.2021\test.xlsx
Update:
The issue source is the native_concat method in jinja's NativeEnvironment.
The method returns the literal_eval of the input.
try:
return literal_eval(out)
Reproduce:
path = '"G:\\Shared drives\\2 test\\2021\\08.2021\\test.xlsx"'
print(literal_eval(path))
> G:\Shared drives test18.2021 est.xlsx
print(repr(literal_eval(path)))
> 'G:\\Shared drives\x02 test\x821\x008.2021\test.xlsx'
\ starts an escape sequence in a Python string literals. To prevent this, you have roughly four options:
Properly escape your string. That is, double up every path separator backslash: '"G:\\Shared drives\\2 test\\2021\\08.2021\test.xlsx"'
Use raw strings to prevent escape sequences from being interpreted: r'G:\Shared drives\2 test\2021\08.2021\test.xls'
Use forward slashes instead of backslashes, which are valid as Windows paths: 'G:/Shared drives/2 test/2021/08.2021/test.xls'
Don’t use string literals in your Python code; instead, read the string from some other source (e.g. user input or a file).
s = "C:\Program Files\norton\appx"
print(s)
s = r"C:\Program Files\norton\appx"
print(s)
I've raised the issue to Jinja, and got the following answer:
https://github.com/pallets/jinja/issues/1518#issuecomment-950326763
"This is due to the way escapes work when parsing Python strings. You need to double up the \\"
Indeed, doubling the backslashes did the trick:
path = '"G:\\\\Shared drives\\\\2 test\\\\2021\\\\08.2021\\\\test.xlsx"'
print(literal_eval(path))
# output:
> G:\Shared drives\2 test\2021\08.2021\test.xlsx
And if I want to keep the double quotes:
path = '"\\"G:\\\\Shared drives\\\\2 test\\\\2021\\\\08.2021\\\\test.xlsx\\""'
print(literal_eval(path))
# output
> "G:\Shared drives\2 test\2021\08.2021\test.xlsx"

Absolute folder path in python

I cannot get this code to work for me:
import os
# Define folder to search
searchFolder = "C:\Users\rohrl\OneDrive\Python\PictureCompare\MixedPictures"
os.chdir(searchFolder)
print(os.curdir)
I keep on getting a Unicode error on line 4. What am I doing wrong? I'm on a Windows PC.
The "\" character in Python is a string escape, and it introduces shortcuts for certain string characters. For example the string "\n" doesn't contain the characters \ and n. It contains a newline character. Windows paths always cause this trouble in Python. When Python sees "\U", it's looking for some unicode escape that doesn't exist.
You can use raw strings in Python by prepending the string with r.
searchFolder = r"C:\Users\rohrl\OneDrive\Python\PictureCompare\MixedPictures"
Or you can get in the habit of using double \\. Python reads \\ as a single \.
searchFolder = "C:\\Users\\rohrl\\OneDrive\\Python\\PictureCompare\\MixedPictures"
You need to escape the backslash - or use slashes.
Also I suggest You look at the pathlib Library (it does not help in this short example, but pathlib makes it more pythonic to work with file system objects) :
import os
import pathlib
# variant 1 - raw string
str_search_folder = r"C:\Users\rohrl\OneDrive\Python\PictureCompare\MixedPictures"
# variant 2 - escaping the backslash
str_search_folder = "C:\\Users\\rohrl\\OneDrive\\Python\\PictureCompare\\MixedPictures"
# variant 3 - my prefered, use slashes
str_search_folder = "C:/Users/rohrl/OneDrive/Python/PictureCompare/MixedPictures"
path_search_dir = pathlib.Path(str_search_folder)
os.chdir(path_search_dir)
# variant 1
print(os.curdir)
# variant 2
print(path_search_dir.cwd())

How to use python to convert a backslash in to forward slash for naming the filepaths in windows OS?

I have a problem in converting all the back slashes into forward slashes using Python.
I tried using the os.sep function as well as the string.replace() function to accomplish my task. It wasn't 100% successful in doing that
import os
pathA = 'V:\Gowtham\2019\Python\DailyStandup.txt'
newpathA = pathA.replace(os.sep,'/')
print(newpathA)
Expected Output:
'V:/Gowtham/2019/Python/DailyStandup.txt'
Actual Output:
'V:/Gowtham\x819/Python/DailyStandup.txt'
I am not able to get why the number 2019 is converted in to x819. Could someone help me on this?
Your issue is already in pathA: if you print it out, you'll see that it already as this \x81 since \201 means a character defined by the octal number 201 which is 81 in hexadecimal (\x81). For more information, you can take a look at the definition of string literals.
The quick solution is to use raw strings (r'V:\....'). But you should take a look at the pathlib module.
Using the raw string leads to the correct answer for me.
import os
pathA = r'V:\Gowtham\2019\Python\DailyStandup.txt'
newpathA = pathA.replace(os.sep,'/')
print(newpathA)
OutPut:
V:/Gowtham/2019/Python/DailyStandup.txt
Try this, Using raw r'your-string' string format.
>>> import os
>>> pathA = r'V:\Gowtham\2019\Python\DailyStandup.txt' # raw string format
>>> newpathA = pathA.replace(os.sep,'/')
Output:
>>> print(newpathA)
V:/Gowtham/2019/Python/DailyStandup.txt

I wanted to rename '\' with '\\' in python example:C:\Users\Performance_stats.xlsx with C:\\Users\\Performance_stats.xlsx

I try to rename '\' with '\\'by using below code but it is showing the error as
SyntaxError: unexpected character after line continuation character
String = "C\users\stat.csv"
String.replace('\','\\')
SyntaxError: unexpected character after line continuation character
Can someone help how to get the output as
"C\\users\\stat.csv" with rename function.
You should make use of the pathlib in python.
from pathlib import Path, PureWindowsPath
# I've explicitly declared my path as being in Windows format, so I can use forward slashes in it.
filename = PureWindowsPath("source_data\\text_files\\raw_data.txt")
# Convert path to the right format for the current operating system
correct_path = Path(filename)
print(correct_path)
# prints "source_data/text_files/raw_data.txt" on Mac and Linux
# prints "source_data\text_files\raw_data.txt" on Windows
To read more, you can refer to this article.
You need to escape each\
String = "C\\users\\stat.csv"
x=String.replace('\\','\\\\ ')
print (x==String)
print (x)
Output as follows:
C\\ users\\ stat.csv

Resources