473,396 Members | 1,767 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,396 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 1540
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:
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.