#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    12

    Questions about string parsing with Python


    Hi,

    I'm a newbie too Python for the most part. I've finally sat down and decided I'm going to dig in and become freinds with the snake. It's gone rather well so far.

    Here is what I'm doing. To help learn Python, I needed a direction to go in. That said, I decided to rewrite a script that I created for randomly switching my desktop background at a specified interval. I've done it for the most part, but there is one area that's been overlooked so far. When reading through the image directories, I first want to check that the potential background files are indeed image files. In PHP I used...

    PHP Code:
    if( (stristr($entry'.png')) || (stristr($entry'.jpg')) || (stristr($entry'.gif')) ) 
    In short, i'm hoping there is a stristr() equivalent in Python. So far I haven't found it. Is regex the only option?

    Cheers,
    BDKR
  2. #2
  3. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    11
    I don't know PHP, but try this:
    Code:
    entry = 'joe.jpg'
    if '.jpg' in joe:
       do_something()
    The in operator will check if '.jpg' is included in joe.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    13
    Yeah that'll work for your purpose though the str .find() method is the more direct replacement.
  6. #4
  7. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    12
    Originally posted by SolarBear
    I don't know PHP, but try this:
    Code:
    entry = 'joe.jpg'
    if '.jpg' in joe:
       do_something()
    The in operator will check if '.jpg' is included in joe.
    This doesn't seem to work. I'll try the str.find() method.

    Cheers,
    BDKR
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    As solar said you can use the in keyword to check for the presence of that substring. However if you're planning to check a files type its much safer to use the endswith() method.

    Code:
    >>> string = 'picture.jpg'
    >>> if string.endswith('.jpg'):
    ...     print True
    ...     
    True
    >>>
    you can also use slices to cut the string in any way you want. One of my favourate parts of Python...

    Code:
    >>> string[:3]
    'pic'
    >>> string[3:]
    'ture.jpg'
    >>> string[:-4]
    'picture'
    >>> s[-4:]
    '.jpg'
    >>>
    Check out the Python docs for more info on string methods and slides...

    http://www.python.org/doc/2.3.3/

    or type help in your python shell!

    Incase you dont already know, and i expect you do. the Python equivilent to || is 'or'.

    Mark.
    programming language development: www.netytan.com Hula

  10. #6
  11. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    12
    Originally posted by Strike
    Yeah that'll work for your purpose though the str .find() method is the more direct replacement.
    Is there a way to make this ignore case?

    Sorry to be a bother... I hate being a newbie that's having trouble finding what I need.

    Cheers,
    BDKR
  12. #7
  13. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ah, not a bother really. You will need Python 2.3 to use the 'in' keyword, also, i think solar ment..

    Code:
    entry = 'joe.jpg'
    if '.jpg' in entry:
        #do something; add your own code here.
    I agree with strike, find() is great for searchin substrings and more useful. But i'd still use endswith() for file extentions.

    if you want to make this case insensative then you can either change the case for checking or check for '.JPG' or '.jpg' although the first is more flexable!

    Code:
    >>> string.upper()
    'PICTURE.JPG'
    >>> string.lower()
    'picture.jpg'
    >>> string.title()
    'Picture.Jpg'
    >>>
    Edit: added example.

    Mark.
    Last edited by netytan; February 8th, 2004 at 04:18 PM.
    programming language development: www.netytan.com Hula

  14. #8
  15. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    12
    Originally posted by netytan
    You will need Python 2.3 to use the 'in' keyword, also, i think solar ment..

    Code:
    entry = 'joe.jpg'
    if '.jpg' in entry:
        #do something; add your own code here.
    I agree with strike, find() is great for searchin substrings and more useful. But i'd still use endswith() for file extentions.

    if you want to make this case insensative then you can either change the case for checking or check for '.JPG' or '.jpg' although the first is more flexable!

    Mark.
    I'm currently converting to lower case and testing. However, I'm going to check out endswitch.

    Thanx for the feedback!

    Cheers,
    BDKR
  16. #9
  17. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    12
    In case anyone is interested, this is what I came up with.

    for item in images:
    item_tmp = item.lower()
    if item_tmp.find('.jpeg') == -1 & item_tmp.find('.png') == -1 & item_tmp.find('.gif') == -1 & item_tmp.find('.jpg') == -1:
    continue

    Any thoughs or comments?

    Now to check out endswitch().

    Cheers,
    BDKR
  18. #10
  19. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    actually this should be writen like.. since 'and' is the same as '&' in PHP.

    Code:
    for item in images:
    	temp = item.lower()
    	if temp.find('.jpeg') == -1 and temp.find('.png') == -1 and temp.find('.gif') == -1 and temp.find('.jpg') == -1:
    		continue
    Please put code inside [ CODE ]...[/ CODE ] tags .

    Mark.
    programming language development: www.netytan.com Hula

  20. #11
  21. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    12
    Originally posted by netytan
    [B]actually this should be writen like.. since 'and' is the same as '&' in PHP.
    That's actually how I did it the first time ('&' and 'and' both will work in PHP as well as Python). Is there a particular reason that 'and' should be used or is it just more consistent with/in the Python commumity?

    Cheers,
    BDKR
  22. #12
  23. onCsdfeu
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Canada
    Posts
    100
    Rep Power
    11
    Mostly because one of Python's greatest assets is readability. While & is known worldwide because of C's influence, I find and to make code a lot more readable and understandable. Otherwise, I don't know of any difference between the two.
  24. #13
  25. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    As i understand it. Unlike in PHP, 'and' and '&' are totaly different.. in python 'and' is a logical operator and '&' is a bitwise and operator.

    So aswell as being cleaner it means something different,

    Mark.
    programming language development: www.netytan.com Hula

  26. #14
  27. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Location
    Tampa, Florida
    Posts
    31
    Rep Power
    12
    Originally posted by netytan
    As i understand it. Unlike in PHP, 'and' and '&' are totaly different.. in python 'and' is a logical operator and '&' is a bitwise and operator.

    Mark.
    OK. I dropped the ball on this one. In php '&&' and 'and' are logical operators, while '&' is a bitwise operator. The curious thing is that when I look back at other things I've written, I used '&&' as opposed to '&'. Hmmmm?

    Cheers,
    BDKR

IMN logo majestic logo threadwatch logo seochat tools logo