473,387 Members | 1,722 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,387 software developers and data experts.

searching a pattern in a file

how to search particular strings in a file. and display it plz send me the python command
Apr 27 '07 #1
3 1456
I'm new to Python myself, but this I can answer. Though it really depends how you are handling that file, something like this works:

Expand|Select|Wrap|Line Numbers
  1. >>> s="hello, how are you?"
  2. >>> print "hell" in s
  3. True
or if it's a complex and varying pattern you're looking for you could use regular expressions,
http://www.amk.ca/python/howto/regex/
Apr 27 '07 #2
bvdet
2,851 Expert Mod 2GB
how to search particular strings in a file. and display it plz send me the python command
This will return a string index list of the occurrences of a substring found in a file:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, i=0):
  2.     i_list = []
  3.     while True:
  4.         try:
  5.             i = s.index(item, i)
  6.             i_list.append(i)
  7.             i += 1
  8.         except:
  9.             break
  10.     return i_list
  11.  
  12. fStr = open('data_file').read()
  13. matchStr = 'some string'
  14. matchList = indexList(fStr, matchStr)
  15. print matchList
In my test case:
>>> [201, 327, 1439, 1488]
Apr 27 '07 #3
This will return a string index list of the occurrences of a substring found in a file:
Expand|Select|Wrap|Line Numbers
  1. def indexList(s, item, i=0):
  2.     i_list = []
  3.     while True:
  4.         try:
  5.             i = s.index(item, i)
  6.             i_list.append(i)
  7.             i += 1
  8.         except:
  9.             break
  10.     return i_list
  11.  
  12. fStr = open('data_file').read()
  13. matchStr = 'some string'
  14. matchList = indexList(fStr, matchStr)
  15. print matchList
In my test case:
>>> [201, 327, 1439, 1488]
thanks dude.. it was so help full to me and i got good ideas to do more
May 2 '07 #4

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

Similar topics

3
by: Christopher R. Barry | last post by:
I need to search and replace multiple words in one pass of an input stream or string. For example, given the input: "The quick brown fox jumped over the lazy dog's back" and given the...
41
by: Richard James | last post by:
Are we looking at the scripting world through Python colored glasses? Has Python development been sleeping while the world of scripting languages has passed us Pythonista's by? On Saturday...
1
by: Henry | last post by:
I have a table that stores a list of zip codes using a varchar column type, and I need to perform some string prefix pattern matching search. Let's say that I have the columns: 94000-1235 94001...
1
by: Pacher R. Dragos | last post by:
I have recently developed an application in c witch acts like a system logger for windows 2003 server domain controllers and my problem is that of the size. In a normal day my program produces...
6
by: Bernie Yaeger | last post by:
I need a little help with an algorithm. Let's say I have an array with 15 items. I want to find "Fern" where there are "Able", "Me Also", "Zimmerman", etc in no particular order. Forget about...
3
by: masudtarek | last post by:
Hi, I am new in Python programming. Can anybody give me any idea about how to detect more than one date and time (like 11/11/2005 , 10-12-2006, 12:30 etc) from a text file and keep them in a list.
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...
3
by: Nafees99 | last post by:
Hi all: I am new to PerlScripting .I got an assignment for parsing number of files at one time.The scenario is that I have 5 txt files in the same folder with different names (like SA1,XA2 ...
5
by: pramodkh | last post by:
Hi All I am trying to match a pattern in a file and insert a line. If the pattern matches then insert a line before the matching pattern line. for example, I have the following content in a...
2
by: Bart Kastermans | last post by:
I have a file in which I am searching for the letter "i" (actually a bit more general than that, arbitrary regular expressions could occur) as long as it does not occur inside an expression that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.