import csv
import io
import re
from django.http import HttpResponse
from django.shortcuts import render
from django.views import generic
from ...forms import CsvUploadForm
import pandas as pd
class CsvImportView(generic.base.View):
def getemailcolumn(self, sample):
for key, val in sample:
print(key, val)
if re.match("^.+@([?)[a-zA-Z0-9-.]+.([a-zA-Z]{2,3}|[0-9]{1,3})(]?)$", val[0]) is not None:
return key
return None
def post(self, request):
form = CsvUploadForm(request.POST, request.FILES)
if form.is_valid():
fieldnames = ['first_name', 'email', 'last_name']
csv = pd.read_csv(request.FILES['csv'])
sample =csv.head();
emailColumn = getemailcolumn(sample)
return HttpResponse(sample)
else:
...
...
执行post方法时,出现名称未定义错误
未定义名称'getemailcolumn'
我想念什么?
答案 0 :(得分:2)
getemailcolumn()
是实例方法。可以通过self
或class instance
进行访问。在您的情况下,您尝试从另一个实例方法访问它。所以应该是
emailColumn = self.getemailcolumn(sample)
答案 1 :(得分:-2)
您需要缩进get和post方法,因为python认为它们不属于CsvImportView类。因此,请缩进它们与您要调用的函数内联。
编辑: 好的,它应该是self.getmailcolumn而不只是函数名