我正在学习django,我不明白这里发生了什么,逻辑是什么


if removepunc == "on":
punctuations = '''!()-[]{};:'",<>./?@#$%^&*_~'''
analyzed = ""
for char in djtext:
if char not in punctuations:
analyzed = analyzed + char
params = {'purpose':'Removed Punctuations', 'analyzed_text': analyzed}
return render(request, 'analyze.html', params)
else:
return HttpResponse("Error")

基本上,这从句子中删除了标点符号,我没有得到程序的循环部分。如果有人能解释一下,那就太好了!由于

你有一个djtext字符串你在上面滚动for:for char in djtext

例如,如果djtext='abc'然后加上for循环,char在'c'之后的'b'之后的第一个循环中是'a'。

在这行if char not in punctuations中,检查如果char不包含标点符号,则将char与分析的字符串连接起来。

结尾:

params = {'purpose':'Removed Punctuations', 'analyzed_text': analyzed}
return render(request, 'analyze.html', params)

响应数据已经准备好,然后在analyze.html文件中返回并从django视图中呈现。

最新更新