在我的.html文件中用Python切片字符串
我正在用Python读取一个Excel文件,并在里面进行搜索。这个搜索会返回用户输入的三个不同的变量。所以,如果用户想搜索“苹果”、“橙子”和“香蕉”,网页就会打印出包含这些词的Excel单元格的整个内容。因为有时候单元格的内容很长,还包含多个句子,所以我想找到搜索的词,并在它前面和后面各切出20个词,如果输出的内容超过这个长度的话。
我把这些值保存为first、second和third,然后把它们发送到数据库,接着再去搜索Excel文件。
在我的.html页面中,我设置了一个地方,让用户输入他们想搜索的词(这些词会存储为数据库中的first、second和third)。
First term:<input type="text" name="first" size="10" style="font-size: 25px; direction: rtl;" value="{{ first }}">
Second Term:<input type="text" name="second" size="10" style="font-size: 25px; direction: rtl;" value="{{ second }}">
third term:<input type="text" name="third" size="10" style="font-size: 25px; direction: rtl;" value="{{ third }}">
然后,打印所有输出的部分是这样的:
{% 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>
我该如何截取{{ value }},按照我之前描述的方式,在搜索词前后各20个词?Value就是输出的内容。
我知道可以从某个数字位置进行切割,比如value|slice:'20:',但我想要从搜索的变量first、second和third开始切割。
2 个回答
1
你可能想要写一个自定义的模板过滤器来执行这个操作,这里有一个你可以使用的函数。我用你提供的开头作为测试文本,用“multiple”这个词作为我的搜索词:
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'
1
我不太了解你的具体情况,不过这里有一个可能的解决办法。
你可以试着把单元格里的内容按照空格分开,把每个单词存到一个数组里。然后找到你要搜索的单词,再取出它前后各20个单词,比如用 words[index - 20:index + 20]
这样的方式。(当然,要记得检查一下索引,以防在你要找的单词前后少于20个单词。)