Question

I'm reading in an excel file and doing a search in it with python. This search returns three different variables that the user inputs. So, if the user wants to search for apple, orange, and banana, the webpage will print out the whole cell of the excel file that that word is located in. What I want to do, since sometimes the cell is really long and has multiple sentences, is to identify where the searched word is and slice it 20 words before and 20 words after, if the output runs longer than that.

I'm saving these values as first ,second and third which are sent to a database, then it searches the excel file.

In my .html page, I have it so that it reads this way in the html portion where the user types in the words they want to search (which are then stored as first, second and third in the DB)

First term:<input type="text" name="first" size="10" style="font-size: 25px; direction: rtl;" value="{{ first }}"> &nbsp;&nbsp;&nbsp;

Second Term:<input type="text" name="second" size="10" style="font-size: 25px; direction: rtl;" value="{{ second }}">  &nbsp;&nbsp;&nbsp;

third term:<input type="text" name="third" size="10" style="font-size: 25px; direction: rtl;" value="{{ third }}"> 

Then, the part where it prints out all the output, is this:

{% for key,value,line in box %}
<form id="form2" name="form2" action="{{URL_ROOT}}/search/" method="post">
{% csrf_token %}
<table class='table' border="0" width="100%">
<tr>

<td class='td-center' width="90%" style="direction: rtl;">{{ value }}</td>

How do I truncate/slice {{ value }}, the way I had described with 20 before and after the searched terms? Value is what is outputted.

I know that I can slice from a numerical place, like value|slice:'20:' but I want it to slice from the searched variables first, second, and third

Was it helpful?

Solution

FYI I'm not familiar with your setup, but here is a possible solution.

Perhaps you could split the cell contents on whitespace and store the words into an array. Then locate your search word and take a slice like words[index - 20:index + 20]. (Of course, be sure to do proper index checking in the event that there are less than 20 words before or after your search word.)

OTHER TIPS

You'll probably want to write a custom template filter to perform the action, here's a function you can use. I used your opening line as a test block of text and the word 'multiple' as my search term:

def from_term(text, term):
    s = text.split(term)
    before = ' '.join(s[0].split()[-20:])
    after = ' '.join(s[1].split()[:20])
    return before + ' ' + term + ' ' + after

>>> text = "I'm reading in an excel file and doing a search in it with python. This search returns three different variables that the user inputs. So, if the user wants to search for apple, orange, and banana, the webpage will print out the whole cell of the excel file that that word is located in. What I want to do, since sometimes the cell is really long and has multiple sentences, is to identify where the searched word is and slice it 20 words before and 20 words after, if the output runs longer than that."
>>> term = 'multiple'
>>> from_term(text, 'multiple')
'that that word is located in. What I want to do, since sometimes the cell is really long and has multiple sentences, is to identify where the searched word is and slice it 20 words before and 20 words after, if'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top