简体   繁体   中英

Find a word after a keyword, then, paste it after a different keyword.

For example, I have a multiple strings in a tex file that looks something like:

"The conditions Heat_Transfer blah blah blah BC"

Using python I would like to:

  1. Find the word after "conditions" (in this case Heat_Transfer)
  2. Then paste Heat_Transfer after BC.

The output string should look like:

"The conditions Heat_Transfer blah blah blah BC Heat_Transfer"

The keywords conditions and BC stay the same for each string, but Heat_Transfer changes.

You first need to split the given string on spaces using the .split() method on string, then you find the index of the keyword and next you place the string next to that index at the end of the string, by simply concatenating the word to the initial string with a space in between.

sentence = "The conditions Heat_Transfer blah blah blah BC" 
keyword = "conditions"

split_sentence = sentence.split()
sentence+=" "+split_sentence[split_sentence.index(keyword)+1]

print sentence
>>> The conditions Heat_Transfer blah blah blah BC Heat_Transfer

Alternatively, you may do all the concatenating operations on the list itself and later join the elements of the string with a space using .join() method.

split_sentence.append(split_sentence[split_sentence.index(keyword)+1])
print " ".join(split_sentence)

This should work.

With the assumption that you are sure that your keyword 'conditions' exist.

str_ = "The conditions Heat_Transfer blah blah blah BC"

list_ = str_.split(" ")
str_ = str_+" "+list_[list_.index("conditions")+1]

Otherwise:

str_ = "The conditions Heat_Transfer blah blah blah BC"
try:
   list_ = str_.split(" ")
   str_ = str_+" "+list_[list_.index("conditions")+1]
except:
   pass
string = string.split() # split string into list

a = string.index(keyword1) # find index of 1st
b = string.index(keyword2) # find index of 2nd

string.insert(b + 1, string[a + 1]) # insert word
string = ' '.join(string) # join list into string

This can be done with regex:

import re 

str = "The conditions Heat_Transfer blah blah blah BC"

match = re.search('conditions\s([a-zA-z_\-0-9]+)', str)

word = match.group(1)

print str + ' ' + word

You can use re to find both and replace keeping all whitespace:

s =  "The conditions Heat_Transfer blah blah blah   BC some other text"
con = re.compile(r"(?<=\bconditions\b)\s+(\S+)")
bc = re.compile(r"(\bBC\b)")
m = con.search(s)
if m:
    print(bc.sub(r"\1"+m.group(), s)) 

The conditions Heat_Transfer blah blah blah   BC Heat_Transfer some other text

If you were going down the splitting route then you can get the indexes in a single pass:

keywords = {"conditions","BC"}
words = s.split(" ")
inds = (i for i, w in enumerate(words) if w in keywords)

a,b = next(inds), next(inds)
print(a, b)

Then simply join where apppropriate:

w = words[a+1]
print(" ".join(["BC " + w if ind == b else wrd for ind,wrd in enumerate(words)]))


The conditions Heat_Transfer blah blah blah   BC Heat_Transfer some other text

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM