Page 3 of 3 FirstFirst 123
Results 21 to 30 of 30

Thread: How to parse a part of a link in python?

  1. #21
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: How to parse a part of a link in python?

    Compare: regex solution:
    PHP Code:
    /[?&]v=([^&]+)/.match url
    result 
    = $
    vs plain string parsing:

    PHP Code:
    result url.split('v=')[1
    if 
    '&' in result:
        
    result result.split('&')[0
    It takes one more line, but is easier to read, unless you are regex expert.

    Of course regex solution scales for more complex cases. But in 80% cases, like this one, regex is overkill, IMHO. YMMV, eat your regex if you want, I don't care anymore.

  2. #22
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to parse a part of a link in python?

    Quote Originally Posted by pmasiar View Post
    Of course regex solution scales for more complex cases. But in 80% cases, like this one, regex is overkill, IMHO. YMMV, eat your regex if you want, I don't care anymore.
    Once I get a regex to match correctly, I don't have to care about it anymore.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  3. #23
    Join Date
    Sep 2006
    Beans
    2,914

    Re: How to parse a part of a link in python?

    Quote Originally Posted by slavik View Post
    Once I get a regex to match correctly, I don't have to care about it anymore.
    that's quite untrue isn't it? how do you know your program specs won't change in future?

  4. #24
    WW is offline Iced Blended Vanilla Crème Ubuntu
    Join Date
    Oct 2004
    Beans
    1,532

    Re: How to parse a part of a link in python?

    The structure is simple enough; regex does seem like overkill. This python function gets all the assigned values into a dict. Python gurus could probably simplify it even more:

    Code:
    # parse_url_opts.py
    
    def parse_url_opts(str):
        (url,opts_str) = str.split("?")
        opts_list = opts_str.split("&")
        opts_pairs = [s.split("=") for s in opts_list]
        return dict(opts_pairs)
    Example:
    >>> from parse_url_opts import *
    >>> z = parse_url_opts("http://www.youtube.com/watch?v=ngjr6IkDLxg&feature=related")
    >>> z.keys()
    ['feature', 'v']
    >>> z['feature']
    'related'
    >>> z['v']
    'ngjr6IkDLxg'
    >>>
    (Something like this might already exist in the cgi module.)

  5. #25
    Join Date
    Jun 2006
    Location
    Gwangju, Korea
    Beans
    3,479

    Re: How to parse a part of a link in python?

    Quote Originally Posted by ghostdog74 View Post
    if a simple regex handles all variations well, it most probably would not be simple anymore, don't you agree?
    Not necessarily. The regex I wrote above is quite simple.

  6. #26
    Join Date
    Sep 2006
    Beans
    2,914

    Re: How to parse a part of a link in python?

    Quote Originally Posted by mssever View Post
    Not necessarily. The regex I wrote above is quite simple.
    yes for this simple case. like you mentioned, if the url contains different kinds of query strings and not just standard "v=xxxxxx", then a simple regex can become quite complex if all variations are to be handled.

  7. #27
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: How to parse a part of a link in python?

    Quote Originally Posted by WW View Post
    Code:
    # parse_url_opts.py
    
    def parse_url_opts(str):
        (url,opts_str) = str.split("?")
        opts_list = opts_str.split("&")
        opts_pairs = [s.split("=") for s in opts_list]
        return dict(opts_pairs)
    the Perl version:
    Code:
    sub parse_url_opts {
      my $url = shift;
      (my $base, my $opts) = split /\?/, $url;
      my @opt_list = split /&/, $opts;
      my %opt_pairs = map { split /=/, $_ } @opt_list;
      return %opt_pairs;
    }
    the only real difference is how the subroutines get their arguments and in Perl, the map is explicit.
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  8. #28
    Join Date
    Apr 2007
    Beans
    14,781

    Re: How to parse a part of a link in python?

    Quote Originally Posted by slavik View Post
    the Perl version:
    Code:
    sub parse_url_opts {
      my $url = shift;
      (my $base, my $opts) = split /\?/, $url;
      my @opt_list = split /&/, $opts;
      my %opt_pairs = map { split /=/, $_ } @opt_list;
      return %opt_pairs;
    }
    the only real difference is how the subroutines get their arguments and in Perl, the map is explicit.
    There is also a "map" function in Python. Although I don't normally use it, it may be used in a way like yours (I just got up and didn't really read the code, except to say the Perl example is very different visually from the Python)

  9. #29
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: How to parse a part of a link in python?

    map will be dropped in Py3K IIRC. List comprehension does the same in a more obvious and readable way.

  10. #30
    Join Date
    Apr 2007
    Beans
    14,781

    Re: How to parse a part of a link in python?

    Quote Originally Posted by pmasiar View Post
    map will be dropped in Py3K IIRC. List comprehension does the same in a more obvious and readable way.
    http://www.artima.com/weblogs/viewpost.jsp?thread=98196

    Quote Originally Posted by GvR
    Update: lambda, filter and map will stay (the latter two with small changes, returning iterators instead of lists). Only reduce will be removed from the 3.0 standard library. You can import it from functools.

Page 3 of 3 FirstFirst 123

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •