471,337 Members | 1,017 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,337 software developers and data experts.

searching within a file

22
Does anyone know where I can read up on how to search for a particular object in a file? For example, search for a particular word in a dictionary?
Jul 25 '07 #1
7 1427
Expand|Select|Wrap|Line Numbers
  1. file("file name.txt", 'rb')
  2. fil.find("word")
Jul 25 '07 #2
bvdet
2,851 Expert Mod 2GB
Not much to it really:
Expand|Select|Wrap|Line Numbers
  1. >>> f = open(r'C:\SDS2_7.0\jobs\60801_Baylor\macro\DeepEllumRad1.txt')
  2. >>> for line in f:
  3. ...     if 'Deep Ellum' in line:
  4. ...         print line
  5. ...         
  6. # Deep Ellum radii and cons lines at Canopy 1 (B-D)
  7.  
  8. >>> f.close()
  9. >>> 
Do you have a specific problem you are trying to solve?
Jul 26 '07 #3
LolaT
22
Not much to it really:
Expand|Select|Wrap|Line Numbers
  1. >>> f = open(r'C:\SDS2_7.0\jobs\60801_Baylor\macro\DeepEllumRad1.txt')
  2. >>> for line in f:
  3. ...     if 'Deep Ellum' in line:
  4. ...         print line
  5. ...         
  6. # Deep Ellum radii and cons lines at Canopy 1 (B-D)
  7.  
  8. >>> f.close()
  9. >>> 
Do you have a specific problem you are trying to solve?

Thanks for the advice.
I'm trying to create a spell checker which can spell check either a single word or a file depending on what the user chooses. The spell checker is really simple though, it doesn't point out a word that is spelled wrong, however it checks two different files to see if the word(s) is/are typed in are contained in either file. One of the files is a dictionary file with a list of words, and the other file is the user dictionary, created by the user.
Jul 26 '07 #4
bvdet
2,851 Expert Mod 2GB
Thanks for the advice.
I'm trying to create a spell checker which can spell check either a single word or a file depending on what the user chooses. The spell checker is really simple though, it doesn't point out a word that is spelled wrong, however it checks two different files to see if the word(s) is/are typed in are contained in either file. One of the files is a dictionary file with a list of words, and the other file is the user dictionary, created by the user.
You are welcome. Let us know how the project goes for you.
Jul 26 '07 #5
Expand|Select|Wrap|Line Numbers
  1. import re
  2. authorpattern = "<meta name="AUTHOR" content="(.*?)"><meta name="
  3. patt = re.compile(authorpattern,re.IGNORECASE)
  4.  
  5. authors = re.findall(patt,data)
  6.  
  7. or if you need only 1 author
  8.  
  9. amatch = re.search(source.authorpattern,data,re.IGNORECASE)
  10. author = amatch.group(1)
Jul 27 '07 #6
bartonc
6,596 Expert 4TB
Expand|Select|Wrap|Line Numbers
  1. import re
  2. authorpattern = "<meta name="AUTHOR" content="(.*?)"><meta name="
  3. patt = re.compile(authorpattern,re.IGNORECASE)
  4.  
  5. authors = re.findall(patt,data)
  6.  
  7. or if you need only 1 author
  8.  
  9. amatch = re.search(source.authorpattern,data,re.IGNORECASE)
  10. author = amatch.group(1)
Hello, annonymous! I'm impressed: 49 posts; C++, Java, MySQL, now Python and a regex to boot.

I welcome your contributions here in the Python forum. I must, however, that you use [code] tags any time that you post executable lines. And
Expand|Select|Wrap|Line Numbers
  1. # Adding =python to them makes them look even better.
Thank you.
Jul 27 '07 #7
LolaT
22
thanks again to everyone for their help.
i'm encountering a major problem with my project though.
i'm supposed to take the file named dict.txt which has quite the amount of words
and turn it into a list.
i know how to do it (at least i think so) but my program keeps shutting down, i'm guessing because the file is pretty large.
any suggestions?
Jul 27 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

4 posts views Thread by James | last post: by
5 posts views Thread by Alan Mackenzie | last post: by
2 posts views Thread by kids | last post: by
14 posts views Thread by Walter Dnes (delete the 'z' to get my real address | last post: by
1 post views Thread by silver_animal | last post: by
29 posts views Thread by jaysherby | last post: by
3 posts views Thread by Ahmad Jalil Qarshi | last post: by
3 posts views Thread by Gene Kelley | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.