I am trying to get a file as an input and search for a special character. I am giving from and to keys as input. if the to keyword is in the next line i should print till the to keyword is found.
for line in contents:
if line.startswith("*CHI: ") :
line = line.strip("*")
tokenize = line.split()
p.token_filter(tokenize)
say i have a file:
*CHI: (hi) new [/] friend [//] there [/-] [ bch] [ bch ] /[]/ [/=]
<new>
<there>.
%mod: hi there.
*CHI: <dude>
<there>
*CHI: &=sighs <and instead the> [//] and then the gira?e got it and gave it to the elephant .
*CHI: <he> [/] <he> [/] he hold it .
*CHI: then [/-] the doctor give the [/] money to the man
*CHI: and (i)s then (.) the little (.) gira?e is crying because it (i)s sinking
By using the above code i am getting the output as follows:
['new', '[/]', 'friend', '[//]', 'there', 'bch', '/[]/']
['dude', 'dude']
['and', 'and', 'instead', 'the', 'the', '[//]', 'and', 'then', 'the', 'gira?e', 'got', 'it', 'and', 'gave', 'it', 'to', 'the', 'elephant', '.']
['he', 'he', '[/]', 'he', 'he', '[/]', 'he', 'hold', 'it', '.']
['then', 'the', 'doctor', 'give', 'the', '[/]', 'money', 'to', 'the', 'man']
['and', 'then', '(.)', 'the', 'little', '(.)', 'gira?e', 'is', 'crying', 'because', 'it', 'sinking']
My another aim is i should print ['new', '[/]', 'friend', '[//]', 'there', 'bch', '/[]/' 'new' 'there' '.' ]
for arbitrary text you can use regex:
>>> import re
>>> text = "*foo* bar *foobar*"
>>> re.findall("\*[^/*]*\*", text)
['*foo*', '*foobar*']
To get rid of the asterisks:
>>> [s.replace("*", "") for s in re.findall("\*[^/*]*\*", text)]
['foo', 'foobar']
If you can read the file and convert it into a string. We can use
string = "123123STRINGabcabc"
def find_between( string, first, last ):
try:
start = string.index( first ) + len( first )
end = string.index( last, start )
return string[start:end]
except ValueError:
return ""
print find_between( string, "123", "abc" )
gives
123STRING
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.