|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now! |
|
#1
|
||||
|
||||
|
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:
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
|
||||
|
||||
|
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. |
|
#3
|
|||
|
|||
|
Yeah that'll work for your purpose though the str .find() method is the more direct replacement.
|
|
#4
|
||||
|
||||
|
Quote:
This doesn't seem to work. I'll try the str.find() method. Cheers, BDKR |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
||||
|
||||
|
Quote:
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 |
|
#7
|
||||
|
||||
|
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. |
|
#8
|
||||
|
||||
|
Quote:
I'm currently converting to lower case and testing. However, I'm going to check out endswitch. Thanx for the feedback! Cheers, BDKR |
|
#9
|
||||
|
||||
|
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 |
|
#10
|
||||
|
||||
|
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. |
|
#11
|
||||
|
||||
|
Quote:
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 |
|
#12
|
||||
|
||||
|
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.
|
|
#13
|
||||
|
||||
|
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. |
|
#14
|
||||
|
||||
|
Quote:
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 |