LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-31-2009, 02:19 PM   #1
PiNPOiNT
LQ Newbie
 
Registered: Apr 2009
Posts: 15

Rep: Reputation: 0
Python: finding substring code not working :(


Code:
#!/usr/bin/env python
import os, sys
file = open("find.html")
myString = file.read()
mySubString=myString[myString.find("Results"):myString.find("title")]
print mySubString
I'm probably doing this a backwards way, but I'm trying to find a specific set of characters from a html document, everything between the words "Results" and the first occurrence of "title". However it keeps coming back blank.

It seems to start at "Results" fine, and depending on if i change the word title to some other set of characters that can be found in the html file, it will work.

Also, this code seems to pick up everything all the way to the last occurrence. How do i limit it to just the first time?

Thanks
 
Old 08-31-2009, 08:34 PM   #2
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
untested
Code:
import os, sys
file = open("find.html")
myString = file.read()
ind=myString.index("title")
myString = myString[:ind]
rind = myString.index("Results")
print myString[rind:]
 
  


Reply



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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Python Parse Code snowman81 Programming 3 01-24-2009 10:50 AM
php preg_replace substring of a substring senyahnoj Programming 5 12-08-2006 11:31 AM
easy python code JoeUser11 Programming 1 01-21-2005 01:40 AM
Finding source code for Linux waynefrank Linux - Newbie 4 01-07-2005 07:24 AM
Trouble Python Code Gerardoj Programming 0 11-30-2003 04:31 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:08 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration