python - Q : Django - how to return data from other function on views. py

标签 python django

我正在研究字符串操作,这就是我想做的
- 我创建了一个函数 gettext(request) 用于从 *.txt 文件获取文本,这是代码

def gettext(request):
  if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
        f = request.FILES['file']
        for chunk in f.chunks():
            text = chunk

        return render(request, 'ecs/index.html', {'text': text, 'form': form})      
    else:
        form = UploadFileForm()
return render_to_response('ecs/index.html', {'form': form})

然后我想将存储在 text 变量上的数据获取到一个函数,称为预处理

def preprocessing(text):
    pp = Preprocess()

    wordTokenize = pp.tokenizing(text)

    return wordTokenize

如何使用 preprocessing(text) 方法来处理 gettext 方法中 text 变量的数据。
我尝试了一些技巧,但仍然没有进展。

最佳答案

将预处理函数从任何地方导入到包含 gettext 的文件中,然后执行以下操作:

def gettext(request):
  if request.method == 'POST':
    form = UploadFileForm(request.POST, request.FILES)
    if form.is_valid():
        f = request.FILES['file']
        for chunk in f.chunks():
            text = chunk
            text = preprocessing(text)
        return render(request, 'ecs/index.html', {'text': text, 'form': form})      
    else:
        form = UploadFileForm()
return render_to_response('ecs/index.html', {'form': form})

关于python - Q : Django - how to return data from other function on views. py,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26690949/

相关文章:

javascript - 从 Django View 打开新选项卡的命令

Python os.walk topdown true 与正则表达式

python - 在 Python 线程中使用 Intvar/DoubleVar 是否安全?

python - 从 HTML 页面动态提取数据

python - 如何修改django rest框架发送的响应

Python 请求,在访问 API 时不断出现 401 错误

python - 将数据保存到两个独立的 Parse 应用程序中

Python 循环随机数

python - 将函数应用于 pandas 中的 loc 调用会导致底层数据帧发生更改吗?

python - create() 需要 1 个位置参数但给出了 2 个? Django