473,405 Members | 2,171 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,405 software developers and data experts.

Character index in line

Hi, friends I want to do a bit advanced search in a file
I want to get a word and its previous and next words
I think I need the index of my search word or there any other solutions

I am waiting your suggestions, thanks
Aug 3 '07 #1
3 2171
6,596 Expert 4TB
Hi, friends I want to do a bit advanced search in a file
I want to get a word and its previous and next words
I think I need the index of my search word or there any other solutions

I am waiting your suggestions, thanks
A regular expression employing groups would do the trick. Basically you'd write a pattern that matched any whole word followed by your keyword followed by any word and assign them each to a group.
Aug 3 '07 #2
2,851 Expert Mod 2GB
Following are a couple of ways:
Expand|Select|Wrap|Line Numbers
  1. s = 'Try to find the words preceding and following a key word.'
  2. keyw = 'preceding'
  3. patt = re.compile(r'[a-zA-Z]+ (?=%s)|%s|(?<=%s) [a-zA-Z]+' % (keyw, keyw, keyw))
  4. print [w.strip() for w in patt.findall(s)]
  6. sList = s.split()
  7. i = sList.index(keyw)
  8. words = [sList[i-1], sList[i], sList[i+1]]
  9. print words
>>> ['words', 'preceding', 'and']
['words', 'preceding', 'and']
Aug 3 '07 #3
511 Expert 256MB
Expand|Select|Wrap|Line Numbers
  1. >>> alist=['first','second' ,'search','next','nexttwo']
  2. >>> alist.index("search")
  3. 2
  4. >>> alist[alist.index("search") + 1 ]
  5. 'next'
  6. >>> alist[alist.index("search") - 1 ]
  7. 'second'
  8. >>>
Aug 3 '07 #4

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

Similar topics

by: Michael Loomington | last post by:
Couldn't find anything here http://www.python.org/doc/2.3.2/lib/string-methods.html I need to cycle through lines in a file that contain words and compare it to a string thats given, call it SSS...
by: dont bother | last post by:
Hi Buddies, I am facing this problem and I dont know what to use as EOF in python: I want to read a file, and put all the individual words in a dictionary with their index: For example if the...
by: Haines Brown | last post by:
I'm having trouble finding the character entity for the French abbreviation for "number" (capital N followed by a small supercript o, period). My references are not listing it. Where would I...
by: Colin Green | last post by:
OK here's is what I wish to do. I have an XML file that I want to read into an XmlDocument. I then want to be able to interrogate the XmlNodes to find both their start AND end character positions...
by: Tony Tortora | last post by:
I am writing and add on application. The application uses Unique IDs and they are stored in Base 26 (ie 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ). I am having trouble reading the decimal value of a...
by: garyusenet | last post by:
I'm working on a data file and can't find any common delimmiters in the file to indicate the end of one row of data and the start of the next. Rows are not on individual lines but run accross...
by: Ivan | last post by:
Hi, What is the best syntax to use a char to index into an array. /////////////////////////////////// For example int data; data = 1;
by: =?Utf-8?B?YXVsZGg=?= | last post by:
i have come across a situation in my project where i read a text file with some characters greater than hex 0x7f. i need to write character (0xE0) to a new file as an exception. however when i...
by: Andrus | last post by:
I use Winforms RichTextBox control to edit scripts. Scripts are plain ascii texts. When error occurs, script engine returns character position of error in code as integer. How to position...
by: zman77 | last post by:
Hello, I have a bunch of Image Buttons in my code. When I leave them as they are, I can build my project without problems. However, when I add OnClick to an image button, I get the error: Too...
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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...
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...
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,...
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,...
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: 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...
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: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.