在python中通过反斜杠拆分字符串
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25047976/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Split a string by backslash in python
提问by user3184086
Simple question but I'm struggling with it for too much time. Basically I want to split a string by \ (backslash).
一个简单的问题,但我为此苦苦挣扎了太多时间。基本上我想用\(反斜杠)分割一个字符串。
a = "1"
Tried to escape the the backslash but it doesn't seem to work:
试图逃避反斜杠,但似乎不起作用:
print(a.split('\'))
print(a.split('"\"'))
print(a.split('\'))
print(a.split('"\"'))
I want to get this result:
我想得到这个结果:
['1','2','3','4']
Many thanks in advance
提前谢谢了
采纳答案by Henry Keiter
You have the right idea with escaping the backslashes, but despite how it looks, your input string doesn't actually have any backslashes in it. You need to escape them in the input, too!
您对转义反斜杠有正确的想法,但不管它看起来如何,您的输入字符串实际上并没有任何反斜杠。您也需要在输入中转义它们!
>>> a = "1\2\3\4" # Note the doubled backslashes here!
>>> print(a.split('\')) # Split on '\'
['1', '2', '3', '4']
You could also use a raw string literalfor the input, if it's likely to have many backslashes. This notation is much cleaner to look at (IMO), but it does have some limitations: read the docs!
如果可能有很多反斜杠,您也可以使用原始字符串文字作为输入。这个符号看起来更清晰(IMO),但它确实有一些限制:阅读文档!
>>> a = r"1"
>>> print(a.split('\'))
['1', '2', '3', '4']
If you're getting aelsewhere, and a.split('\\')doesn't appropriately split on the visible backslashes, that means you've got something else in there instead of real backslashes. Try print(repr(a))to see what the "literal" string actually looks like.
如果你在a其他地方,并且a.split('\\')没有在可见的反斜杠上适当地分割,那意味着你在那里有别的东西而不是真正的反斜杠。尝试print(repr(a))查看“文字”字符串的实际外观。
>>> a = '1'
>>> print(a)
1???
>>> print(repr(a))
'1\x02\x03\x04'
>>> b = '1\2\3\4'
>>> print(b)
1
>>> print(repr(b))
'1\2\3\4'
回答by Jonas Raedle
You can split a string by backslash using a.split('\\').
您可以使用a.split('\\').
The reason this is not working in your case is that \xin your assignment a = "1\2\3\4"is interpreted as an octal number. If you prefix the string with r, you will get the intended result.
这在您的情况下不起作用的原因是\x在您的分配中a = "1\2\3\4"被解释为八进制数。如果您在字符串前加上r,您将获得预期的结果。
回答by jparonson
According to this answer:
根据这个答案:
https://stackoverflow.com/a/2081708/3893465
https://stackoverflow.com/a/2081708/3893465
you'll need to escape the backslashes before splitting as such:
在拆分之前,您需要转义反斜杠:
>>> a = "1"
>>> a.encode('string-escape').split("\x")
['1', '02', '03', '04']
回答by Shyam Dwivedi
'\r' does the trick for me
'\r' 对我有用
var = 'a\b\c\d'
var.split('\r')
['a', 'b', 'c', 'd']
回答by Dinesh Kumar
Covering all the special cases like \a,\b, \f, \n, \r, \t, \v (String Literals)
涵盖所有特殊情况,如 \a,\b, \f, \n, \r, \t, \v ( String Literals)
def split_str(str):
ref_dict = {
'\x07':'a',
'\x08':'b',
'\x0C':'f',
'\n':'n',
'\r':'r',
'\t':'t',
'\x0b':'v',
}
res_arr = []
temp = ''
for i in str :
if not i == '\':
if i in ref_dict:
if not temp == "":
res_arr.append(temp)
res_arr.append(ref_dict[i])
temp = ''
else:
temp += i
else:
if not temp == '':
res_arr.append(temp)
temp = ''
res_arr.append(temp)
return res_arr
str = "a\a\b\f\n\r\t\v\c\d\e\f\i"
print(split_str(str))

