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

Character index in line

1
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 2169
bartonc
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
bvdet
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)]
  5.  
  6. sList = s.split()
  7. i = sList.index(keyw)
  8. words = [sList[i-1], sList[i], sList[i+1]]
  9. print words
Output:
>>> ['words', 'preceding', 'and']
['words', 'preceding', 'and']
>>>
Aug 3 '07 #3
ghostdog74
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. >>>
  9.  
Aug 3 '07 #4

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

Similar topics

3
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...
2
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...
38
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...
1
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...
6
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...
24
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...
13
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;
13
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...
5
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...
2
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.