473,224 Members | 1,916 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,224 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 1532
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

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

Similar topics

4
by: James | last post by:
We have a need to search through an entire drive for a specific file name. The process is currently written with recursive loops through each directory and the Scripting.FileSystemObject. Problem...
5
by: Alan Mackenzie | last post by:
I've recently moved onto a C++ project with a large number of directories (several hundred) containing an even larger number of C++ source files. There are vastly more ways in C++ to obfuscate a...
2
by: kids | last post by:
Hi all, i have 2 files, 1 file that store 1000 records of name entries and it's key ID. With the sorting function all the records have been sorted and its correspond key id was store in a sorted...
14
by: Walter Dnes (delete the 'z' to get my real address | last post by:
I took a C course some time ago, but I'm only now beginning to use it, for a personal pet project. My current stumbling-block is finding an efficient way to find a match between the beginning of a...
1
by: silver_animal | last post by:
Greetings, Hopefully this posting is clear to both newsgroups. I have been looking for a way to open a PDF file and automatically locate and go to a certain phrase within the file. The phrase...
29
by: jaysherby | last post by:
I'm new at Python and I need a little advice. Part of the script I'm trying to write needs to be aware of all the files of a certain extension in the script's path and all sub-directories. Can...
1
by: SSJVEGETA | last post by:
Hello, everybody. I have read some examples and manuals for the egrep command for Linux and I don't know if this egrep command is right for the particular files I am searching for. Here is what the...
3
by: Ahmad Jalil Qarshi | last post by:
Hi, I have a text file having size about 2 GB. The text file format is like: Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values Numeric valueAlphaNumeric values For example...
3
by: Gene Kelley | last post by:
Hello PHPers, I'm trying to get at a single line within a text file. For instance, I want to print the line within a the file named "file.txt" that starts with "17:". file.txt contains...
5
by: Sevla | last post by:
hello guys i set this code , and i would like to know if its possible set "quotes" on the searched word. Thank You #word searching program within a txt file #!C:/perl/bin/perl.exe start:
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.