Using "r" for escape sequence when file in Julia - string

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.

Related

extract data between single quotes

trying to extract the data between single quotes
import re
a = 'USA-APA HA-WBS-10.152.08.0/24'
print(re.findall(r'()', a))
expecting the oputput : USA-APA HA-WBS-10.152.08.0/24
What is wrong with ? It is just a string ?
a = 'USA-APA HA-WBS-10.152.08.0/24'
print(a)
Output:
% python3 test.py
USA-APA HA-WBS-10.152.08.0/24
You might want to look at this also regarding quotes and strings:
Single and Double Quotes | Python
I am not very familiar with python but with some quick searching around
I've found that this work
import re
a = 'USA-APA HA-WBS-10.152.08.0/24'
result = re.findall(r'(.*?)', a)
print("".join(result))
I'm pretty sure there are better ways of solving this but I'm not familiar with the language

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())

python replace \\ with \ in stringpath automatically

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"

multiple variable in python regex

I have seen several related posts and several forums to find an answer for my question, but nothing has come up to what I need.
I am trying to use variable instead of hard-coded values in regex which search for either word in a line.
However i am able to get desired result if i don't use variable.
<http://www.somesite.com/software/sub/a1#Msoffice>
<http://www.somesite.com/software/sub1/a1#vlc>
<http://www.somesite.com/software/sub2/a2#dell>
<http://www.somesite.com/software/sub3/a3#Notepad>
re.search(r"\#Msoffice|#vlc|#Notepad", line)
This regex will return the line which has #Msoffice OR #vlc OR #Notepad.
I tried defining a single variable using re.escape and that worked absolutely fine. However i have tried many combination using | and , (pipe and comma) but no success.
Is there any way i can specify #Msoffice , #vlc and #Notepad in different variables and so later i can change those ?
Thanks in advance!!
If I did understand you the right way you'd like to insert variables in your regex.
You are actually using a raw string using r' ' to make the regex more readable, but if you're using f' ' it allows you to insert any variables using {your_var} then construct your regex as you like:
var1 = '#Msoffice'
var2 = '#vlc'
var3 = '#Notepad'
re.search(f'{var1}|{var2}|{var3}', line)
The most annoying issue is that you will have to add \ to escaped char, to look for \ it will be \\
Hope it helped
import re
lines = ["<http://www.somesite.com/software/sub/a1#Msoffice>",
"<http://www.somesite.com/software/sub1/a1#vlc>",
"<http://www.somesite.com/software/sub2/a2#dell>",
"<http://www.somesite.com/software/sub3/a3#Notepad>"]
for line in lines:
if re.search(r'\b(?:\#{}|\#{}|\#{})\b'.format('Msoffice', 'vlc', 'Notepad'), line):
print(line)
Output :
<http://www.somesite.com/software/sub/a1#Msoffice>
<http://www.somesite.com/software/sub1/a1#vlc>
<http://www.somesite.com/software/sub3/a3#Notepad>

Why is str.translate() returning an error and how can I fix it?

import os
def rename_files():
file_list = os.listdir(r"D:\360Downloads\test")
saved_path = os.getcwd()
os.chdir(r"D:\360Downloads\test")
for file_name in file_list:
os.rename(file_name, file_name.translate(None,"0123456789"))
rename_files()
the error message is TypeError: translate() takes exactly one argument (2 given). How can I format this so that translate() does not return an error?
Hope this helps!
os.rename(file_name,file_name.translate(str.maketrans('','','0123456789')))
or
os.rename(file_name,file_name.translate({ ord(i) : None for i in '0123456789' }))
Explanation:
I think you're using Python 3.x and syntax for Python 2.x. In Python 3.x translate() syntax is
str.translate(table)
which takes only one argument, not like Python 2.x in which translate() syntax is
str.translate(table[, deletechars])
which can takes more than one arguments.
We can make translation table easily using maketrans function.
In this case, In first two parameters, we're replacing nothing to nothing and in third parameter we're specifying which characters to be removed.
We can also make translation table manually using dictionary in which key contains ASCII of before and value contains ASCII of after character.If we want to remove some character it value must be None.
I.e. if we want to replace 'A' with 'a' and remove '1' in string then our dictionary looks like this
{65: 97, 49: None}

Resources