0

Strings to raw strings in list

I'm working on a project in order to learn and develop my code capabilities with Python 3. In this project I need raw strings with paths. Example: rPaths = [r"Path to the app", r"C:\\Program Files (x86)\\MAGIX\\MP3 deluxe 19\\MP3deluxe.exe", r"F:\\VLC\\vlc.exe"] I also need this to be achieved from another list that contains only normal strings: Paths = ["Path to the app", "C:\\Program Files (x86)\\MAGIX\\MP3 deluxe 19\\MP3deluxe.exe", "F:\\VLC\\vlc.exe"] In order to achieve that I tried the following: ( https://code.sololearn.com/c6MUJK7pgSup/#py ) rPaths1 = "%r"%Paths rPaths2 = [re.compile(p) for p in Paths] rPaths3 = ["%r"%p for p in Paths] To which the results weren't the desired: >>>print(Paths) #provided ['Path to the app', 'C:\\Program Files (x86)\\MAGIX\\MP3 deluxe 19\\MP3deluxe.exe', 'F:\\VLC\\vlc.exe'] >>>print(rPaths) #desired ['Path to the app', 'C:\\\\Program Files (x86)\\\\MAGIX\\\\MP3 deluxe 19\\\\MP3deluxe.exe', 'F:\\\\VLC\\\\vlc.exe'] >>>print(rPaths1) #try1 ['Path to the app', 'C:\\Program Files (x86)\\MAGIX\\MP3 deluxe 19\\MP3deluxe.exe', 'F:\\VLC\\vlc.exe'] >>>print(rPaths2) #try2 [re.compile('Path to the app'), re.compile('C:\\Program Files (x86)\\MAGIX\\MP3 deluxe 19\\MP3deluxe.exe'), re.compile('F:\\VLC\\vlc.exe')] >>>print(rPaths3) #try3 ["'Path to the app'", "'C:\\\\Program Files (x86)\\\\MAGIX\\\\MP3 deluxe 19\\\\MP3deluxe.exe'", "'F:\\\\VLC\\\\vlc.exe'"] The code works when I manually create the r before every string in a new list (rPaths) or when I double the backslashes. However I wanted a way to add the r before every string automatically. Can anyone help me? I would prefer not to import anything.

28th Mar 2018, 4:17 PM
Bernardo Bernardino Gameiro
Bernardo Bernardino Gameiro - avatar
1 Odpowiedź
0
Have you tried re.escape(...) ? this could help you. >>> re.escape(Paths[1]) 'C\\:\\\\Program\\ Files\\ \\(x86\\)\\\\MAGIX\\\\MP3\\ deluxe\\ 19\\\\MP3deluxe\\.exe'
3rd Apr 2018, 10:28 AM
Loïc Mahé
Loïc Mahé - avatar