The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Questions about string parsing with Python
Discuss Questions about string parsing with Python in the Python Programming forum on Dev Shed. Questions about string parsing with Python Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

February 8th, 2004, 03:23 PM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31
Time spent in forums: 1 h 11 m 29 sec
Reputation 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
|

February 8th, 2004, 03:38 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
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.
|

February 8th, 2004, 03:53 PM
|
|
Contributing User
|
|
Join Date: Dec 2001
Location: Houston, TX
Posts: 383
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
|
|
|
Yeah that'll work for your purpose though the str .find() method is the more direct replacement.
|

February 8th, 2004, 03:55 PM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
|
|
Quote: 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
|

February 8th, 2004, 04:01 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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
|

February 8th, 2004, 04:04 PM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
|
|
Quote: 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
|

February 8th, 2004, 04:12 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

February 8th, 2004, 04:18 PM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
|
|
Quote: 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
|

February 8th, 2004, 04:26 PM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31
Time spent in forums: 1 h 11 m 29 sec
Reputation 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
|

February 8th, 2004, 04:53 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

February 8th, 2004, 05:09 PM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
|
|
Quote: 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
|

February 8th, 2004, 08:58 PM
|
 |
onCsdfeu
|
|
Join Date: Jul 2003
Location: Canada
Posts: 100
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
|
|
|
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.
|

February 9th, 2004, 04:09 AM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
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.
|

February 9th, 2004, 09:30 AM
|
 |
Contributing User
|
|
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
|
|
Quote:
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
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|