【问题标题】:How can I separate this into two strings?我怎样才能把它分成两个字符串?
【发布时间】:2012-10-09 22:24:13
【问题描述】:

我是 Python 新手,我不确定我应该寻找什么,但我向你保证,我已经完成了我的研究,并且仍然为这个简单的问题想出了一个相当丑陋的 20 行长代码块。

我正在使用基于 Pyramid 框架的应用程序处理遍历 URL。

现在,URL 可以是:(url = None)

  1. url = ""
  2. url = "/"
  3. url = "/block_1"
  4. url = "/block_1/"
  5. url = "/block_1/block_2"
  6. url = "/block_1/block_2/"

url 不能包含任何内容。在这种情况下,我希望我的函数返回 False、None 或空列表或元组。 (无所谓。)(匹配选项 0 或 1)

Block_1:这是一个单词,a 到 Z 字符串。不能也不应该包含任何特殊字符。事实上,作为 block_1 获取的内容,应该在字典中(或列表)中,如果未找到,则应该引发并返回错误。如果 block_1 不存在或未找到,如上所述,该函数应返回 False、None 或空列表或元组。 (匹配选项 2 和 3)

Block_2:Block_2 可以是任何东西。为简单起见,它可以包含任何语言的任何字符以及特殊字符,例如:()[]。如果我弄错了,请原谅,但我认为我想要的基本上是它匹配[\pL\pN].*,除了一个例外:它的最后一个字符不能是斜线:既不是"\",也不是"/"。最好,我希望它是a to Z (including all languages' alphabets and their accented characters) along with some other characters from a list(我在上面专门定义了它:()和[])。如果没有给出block_2,它应该有值None,如果它不匹配,它应该返回False。 (匹配上面列出的最后 2 个选项)

我的代码开始于,相当原始,我为此道歉:

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

如果我的代码很糟糕而且过于复杂,我很抱歉。我只想要一种更优雅、更好的方式来做到这一点。

那我该怎么做呢?我应该怎么做才能摆脱这些卡住的代码行?

谢谢。

【问题讨论】:

    标签: python python-2.7 pyramid


    【解决方案1】:

    split('/') 绝对应该使用,它应该可以帮助您解析 URL。

    如果这还不够,应该使用urlparse 来解析

    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']
    

    【讨论】:

      【解决方案2】:

      我会尝试的第一件事是.split() string function

      >>> url = "/block_1/block_2"
      >>> url.split("/")
      ['', 'block_1', 'block_2']
      

      这将返回由/ 字符分隔的字符串组件列表。从那里,您可以使用len() 函数找出列表的长度,并根据您所需的逻辑采取适当的操作。

      【讨论】:

      • 是的,这就是你所需要的,除了一些例外。
      猜你喜欢
      • 1970-01-01
      • 2014-01-28
      • 1970-01-01
      • 2018-03-26
      • 1970-01-01
      • 2021-12-13
      • 2014-12-14
      • 2017-08-28
      • 2020-11-06
      相关资源
      最近更新 更多