【发布时间】:2022-01-24 04:33:12
【问题描述】:
我正在尝试使用regex 将此字符串分隔成一个列表:
-y -hwaccel cuda -threads 8 -loglevel error -hide_banner -stats -i - -c:v hevc_nvenc -rc constqp -preset p7 -qp 18 C:\Users\User\Documents\Python\Smoothie\test 124\Resampled_vid.mp4
我是用下面的方法来分隔的:
split(r'(?!\\)'+'\s+',f"{Settings[1]}".format(Input=InFile,Output=OutFile))
Output:
['-y', '-hwaccel', 'cuda', '-threads', '8', '-loglevel', 'error', '-hide_banner', '-stats', '-i', '-', '-c:v', 'hevc_nvenc', '-rc', 'constqp', '-preset', 'p7', '-qp', '18', 'C:\\Users\\User\\Documents\\Python\\Smoothie\\test', '124\\Resampled_vid.mp4']
期望的输出:
['-y', '-hwaccel', 'cuda', '-threads', '8', '-loglevel', 'error', '-hide_banner', '-stats', '-i', '-', '-c:v', 'hevc_nvenc', '-rc', 'constqp', '-preset', 'p7', '-qp', '18', 'C:\\Users\\User\\Documents\\Python\\Smoothie\\test 124\\Resampled_vid.mp4']
无论如何,我可以完全避免在文件路径处拆分?
【问题讨论】:
-
你需要一个解析器,而不是一个正则表达式。
-
文件路径总是在字符串的末尾吗?
-
不,它可以在字符串中的任何位置。
标签: python regex list split whitespace