我怎样才能把它分成两个字符串? [英] How can I separate this into two strings?
问题描述
我是 Python 新手,我不确定我应该寻找什么,但我向您保证,我已经完成了我的研究,并且仍然为这个简单的问题想出了一段相当丑陋的 20 行长代码块.
我正在使用基于 Pyramid 框架的应用程序处理遍历 URL.
I am processing a traversal URL with my app based on Pyramid framework.
现在,URL 可以是这些:(url = None)
Now, the URL can be these: (url = None)
- url = ""
- url = "/"
- url = "/block_1"
- url = "/block_1/"
- url = "/block_1/block_2"
- url = "/block_1/block_2/"
网址不能包含任何内容.在这种情况下,我希望我的函数返回 False、None 或空列表或元组.(不管哪个.)(匹配选项 0 或 1)
The url can contain nothing. In this case, I want my function to return False, None, or an empty list or tuple. (Does not matter which.) (matching options 0 or 1)
Block_1:这是一个单词,一个到 Z 的字符串.不能也不应该包含任何特殊字符.实际上,作为 block_1 获取的内容,应该在字典中(或列表),如果找不到,则应引发并返回错误.如果 block_1 不存在或未找到,则如上所述,该函数应返回 False、None 或空列表或元组.(匹配选项 2 和 3)
Block_1: This is a single word, a to Z string. Can not and should not contain any special characters. In fact, what's fetched as block_1, should be in a dictionary (or a list) and if not found, an error should be raised and returned. If block_1 is not there or not found, the function, as stated above, should return False, None or empty list or tuple. (matching options 2 and 3)
Block_2:Block_2 可以是任何东西.为简单起见,它可以包含任何语言的任何字符以及特殊字符,例如:()[].如果我弄错了,请原谅我,但我认为我想要的基本上是它匹配 [\pL\pN].*
,但有一个例外:它的最后一个字符不能是斜杠:两者都不是"\"
或 "/"
.最好,我希望它是 a 到 Z(包括所有语言的字母及其重音字符)以及列表中的一些其他字符
(我如上所述特别定义:() 和 []).如果没有给出 block_2,它的值应该是 None,如果不匹配,它应该返回 False.(匹配上面列出的最后 2 个选项)
Block_2: Block_2 can be anything. For simplicity, it can contain any characters of any languages along with special characters such as: ()[]. Excuse me if I'm mistaken but I think what I want is basically for it to match [\pL\pN].*
, with one exception: its last character can not be either of slashes: neither "\"
nor "/"
. Preferably, I would like it to be a to Z (including all languages' alphabets and their accented characters) along with some other characters from a list
(which I define specially as mentioned above: () and []). If block_2 is not given it should have the value None and if it's not matched, it should return False. (matching last 2 options listed above)
我的代码开始于,相当原始,我为此道歉:
My code starts with, rather primitively for which I apologise:
if not url:
return False
# then goes on evaluating the first charachter to see if it's a /
if fetch[0] == '/':
length = len(url)
#then checks if there's a second / for the block_2
slash_2 = fetch.find('/', 3) # or '/', 1
if slash_2 == -1:
block_1, block_2 = url[1:length].lower(), None
# checks if block_1 is in a dictionary
if not block_1 in the_dict:
return False
else: # if it's there it processes what's remaining
block_1 = fetch[1:slash_2]
block_2 = fetch[slash_2+1:]
# then checks if there's another slash at the end of block_2
if block_2[-1] == '/': # if so it removes it
block_2 = block_2[:-1]
return False # otherwise returns false, which can be () or [] or None
如果我的代码很糟糕并且过于复杂,我很抱歉.我只喜欢一种更优雅、更好的方式来做到这一点.
I'm sorry if my code is terrible and over complicated. I would love nothing more than a more elegant and better way to do this.
那我该怎么办呢?我应该怎么做才能摆脱这些卡住的代码行?
So how can I do it? What should I do to get rid of this jammed lines of code?
谢谢.
推荐答案
split('/')
绝对应该使用,它应该可以帮助您解析 URL.
split('/')
should definitely be used and that should help you parse the URL.
如果这还不够,应该使用urlparse
来解析
If that is not sufficient, urlparse
should be used to parse
urlparse.urlparse(path)
In [31]: url = 'http://stackoverflow.com/questions/12809298/how-can-i-separate-this-into-two-strings/12809315#12809315'
In [32]: urlparse.urlparse(url)
Out[32]: ParseResult(scheme='http', netloc='stackoverflow.com', path='/questions/12809298/how-can-i-separate-this-into-two-strings/12809315', params='', query='', fragment='12809315')
In [33]: a = urlparse.urlparse(url)
In [34]: a.path
Out[34]: '/questions/12809298/how-can-i-separate-this-into-two-strings/12809315'
In [35]: a.path.split('/')
Out[35]:
['',
'questions',
'12809298',
'how-can-i-separate-this-into-two-strings',
'12809315']
这篇关于我怎样才能把它分成两个字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!