- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
如何将带有空格、括号等的文件名解析为变量?例如。
'Album Artist - Song name (feat Musician) [Year]'
到
'Album\ Artist\ \- Song\ name\ \(feat\ Musician\)\ \[Year\]'
我使用 re.escape(filename)
获得了正确的格式。但是,如果我将 re.escape
中的打印存储到一个变量中,它就会恢复为初始命名。我知道我可以使用 "string".replace('x', 'y')
方法。但它对我来说并不安全。
有人知道我该如何解决或解决这个问题吗?顺便说一句,使用 Python 3.5.3。
编辑示例代码:
>>> import re
>>> # this is an example array in the format how my filenames are named stored in files >>> files = ['AA - BB (CC) [DD]', 'EE - FF (GG) [HH]', 'II - JJ (KK) [LL]']
>>> for f in files:
... print(f)
...
AA - BB (CC) [DD]
EE - FF (GG) [HH]
II - JJ (KK) [LL]
>>> for f in files:
... print(re.escape(f))
...
AA\ \-\ BB\ \(CC\)\ \[DD\] # desired format
EE\ \-\ FF\ \(GG\)\ \[HH\]
II\ \-\ JJ\ \(KK\)\ \[LL\]
>>> escaped = re.escape(files[0])
>>> escaped
'AA\\ \\-\\ BB\\ \\(CC\\)\\ \\[DD\\]' # actual result
>>>
最佳答案
潜在的问题听起来像是将可能包含需要作为参数转义的字符的文件名传递给另一个程序。我建议看看 subprocess
.
具体参见frequently used arguments :
args
is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.
例如:
import subprocess
file_names = [r'AA - BB (CC) [DD]', r'EE - FF (GG) [HH]', r'II - JJ (KK) [LL]']
for file_name in file_names:
subprocess.call([r'touch', file_name])
关于Python/Bash - 获取带有转义字符的文件名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47948120/
我有一个 javascript 从用户输入中读取的 URL。这是 JavaScript 代码的一部分: document.getElementById("Snd_Cont_AddrLnk_BG").v
我将如何在 javascript 中转义斜杠// var j = /^(ht|f)tp(s?)://([\w-]+\.)+[\w-]+(/[\w-./?%&=]*)?$;/ 最佳答案 使用 \ 进行转
在解析到这样的对象之前,我要转义 & 和 =: var obb = parseJSON('{"' + text.replace(/&/g, "\",\"").replace(/=/g,"\":\"")
我正在使用 freemarker 生成一个 freemarker 模板。但我需要一些方法来转义 freemarker 标签。 我将如何逃脱 标签或 ${expression} ? 最佳答案 您也可以使
我正在尝试匹配方括号,即 excel 中正则表达式 VBA 中的 []。我正在尝试使用以下代码,但它不起作用。 Public Function IsSpecial(s As String) As L
我通过设置将 PowerShell 添加到我的上下文菜单中: Windows Registry Editor Version 5.00 [HKEY_CLASSES_ROOT\Directory\she
我需要转义 $,因此我需要将所有出现的 $ 替换为 \$ 所以我写了这个方法: // String#replaceAll(String regex, String replacement) publi
我正在格式化我的问题。非常遗憾。这是我的问题的摘要 在 JSP 中我有一个字段 我输入的值类似于“cQN==ujyRMdr+Qi8dO9Xm*eRun+ner==aLTyt?aKmGI” 实际行动
我有一个文本文件,其内容是C:\temp 我想要值 C:\temp替换为从变量定义的不同值 此外,将从批处理文件(windows .cmd)中调用 perl oneliner set CMDDIR=C
有没有办法使用 jTemplates 来转义 {$,这样我就可以在 onBlur 中使用内联 javascript,例如 telegraaf 在 processTemplate 之后得到这个: 谢谢
我正在尝试将 wget 与包含“#”符号的 url 一起使用。无论我做什么来逃避这个角色,它都不起作用。我用过\、' 和 "。但它们都不起作用。有人有什么建议吗? 谢谢! 最佳答案 如果您真的想让它有
我想知道如何从数据库中回显带有 $ 符号的字符串。此时,数据库中的值“Buy one for $5.00”将转换为“Buy one for .00”。 假设该字段的名称为 title,值为 Buy o
我在 mySQL 中有一个查询,旨在返回我们网站上使用的搜索词。是的,这是一个标签云,是的,我知道它是一条鲻鱼 :) 我们有一个管理页面,管理员可以在其中查看搜索词并选择将它们排除在云端之外。这些词进
我有一个文本区域。在其点击事件上。我将其插入数据库中,然后将其显示为元素列表中的第一个元素。问题是。如果我输入""在textarea中,jquery无法正确显示。它显示为空。代码是 var note
我想知道是否有某种字符串前缀,这样 cstring 就可以按原样使用,而不需要我转义所有字符。我不是 100% 确定。我记得一些关于在字符串前加上 @ 符号( char str[] = @"some\
这个问题在这里已经有了答案: How do I escape curly-brace ({}) characters in a string while using .format (or an f
C/C++编译器如何操作源代码中的转义字符["\"]?如何编写用于处理该字符的编译器语法?遇到那个字符后,编译器会做什么? 最佳答案 大多数编译器分为几个部分:编译器前端称为 lexical anal
我计划接受用户输入,并将其插入到一个 div 中 user_content 一个用户提供内容,另一个用户接收内容。 我认为我会遵循的建议来自 https://www.owasp.org/index.p
我有一个这种形式的 url - http:\\/\\/en.wikipedia.org\\/wiki\\/The_Truman_Show。我怎样才能使它成为正常的网址。我试过使用 urllib.unq
我有一个带有转义数据的字符串 escaped_data = '\\x50\\x51' print escaped_data # gives '\x50\x51' 什么 Python 函数会对其进行反转
我是一名优秀的程序员,十分优秀!