Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old February 8th, 2004, 03:23 PM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
  #2  
Old February 8th, 2004, 03:38 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to 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.

Reply With Quote
  #3  
Old February 8th, 2004, 03:53 PM
Strike Strike is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Dec 2001
Location: Houston, TX
Posts: 383 Strike User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 41 m 27 sec
Reputation Power: 12
Send a message via ICQ to Strike Send a message via AIM to Strike Send a message via Yahoo to Strike
Yeah that'll work for your purpose though the str .find() method is the more direct replacement.

Reply With Quote
  #4  
Old February 8th, 2004, 03:55 PM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
  #5  
Old February 8th, 2004, 04:01 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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


Reply With Quote
  #6  
Old February 8th, 2004, 04:04 PM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
  #7  
Old February 8th, 2004, 04:12 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #8  
Old February 8th, 2004, 04:18 PM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
  #9  
Old February 8th, 2004, 04:26 PM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
  #10  
Old February 8th, 2004, 04:53 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #11  
Old February 8th, 2004, 05:09 PM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
  #12  
Old February 8th, 2004, 08:58 PM
SolarBear's Avatar
SolarBear SolarBear is offline
onCsdfeu
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2003
Location: Canada
Posts: 100 SolarBear User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 16 m 21 sec
Reputation Power: 10
Send a message via MSN to SolarBear
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.

Reply With Quote
  #13  
Old February 9th, 2004, 04:09 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,537 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 17 m 47 sec
Reputation Power: 68
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to 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.

So aswell as being cleaner it means something different,

Mark.

Reply With Quote
  #14  
Old February 9th, 2004, 09:30 AM
BDKR's Avatar
BDKR BDKR is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2002
Location: Tampa, Florida
Posts: 31 BDKR User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 11 m 29 sec
Reputation Power: 12
Send a message via AIM to BDKR Send a message via Yahoo to BDKR
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

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Questions about string parsing with Python

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap