list()在appengine中不起作用?

Dhu*_*nth 0 python django google-app-engine

我试图在appengine中使用set函数来准备一个包含唯一元素的列表.当我编写一个python代码时,我遇到了麻烦,这个代码在python shell中工作得很好但不在appengine + django中

这是我打算做的(在IDLE中运行此脚本):

import re
value='   r.dushaynth@gmail.com, dash@ben,,  , abc@ac.com.edu    '
value = value.lower()
value = list(set(re.split('^\s*|\s*,+\s*|\s*$', value)))
if (value[0] == ''):
    value.remove('')
print value       
Run Code Online (Sandbox Code Playgroud)

所需的输出是(在IDLE中获得此输出):

['dash@ben', 'abc@ac.com.edu', 'r.dushaynth@gmail.com']
Run Code Online (Sandbox Code Playgroud)

现在,当我在appengine的views.py文件中执行相同的操作时:

import os
import re
import django
from django.http import HttpResponse
from django.shortcuts import render_to_response # host of other imports also there
def add(request):

    value='   r.dushaynth@gmail.com, dash@ben,,  , abc@ac.com.edu    '
    value = value.lower()
    value = list(set(re.split('^\s*|\s*,+\s*|\s*$', value)))
    if (value[0] == ''):
        value.remove('')


    return render_to_response('sc-actonform.html', {
        'value': value,
    })
Run Code Online (Sandbox Code Playgroud)

我在转到相应页面时遇到此错误(粘贴回溯):

Traceback (most recent call last):
File "G:\Dhushyanth\Google\google_appengine\lib\django\django\core\handlers\base.py" in get_response
  77. response = callback(request, *callback_args, **callback_kwargs)
File "G:\Dhushyanth\development\AppengineProjects\trunk_sanjhachoolha\sandbox\dushyanth\sanjhachoolha\views.py" in add
  148. value = list(set(re.split('^\s*|\s*,+\s*|\s*$', value)))
File "G:\Dhushyanth\development\AppengineProjects\trunk_sanjhachoolha\sandbox\dushyanth\sanjhachoolha\views.py" in list
  208. return respond(request, None, 'sc-base', {'content': responseText})
File "G:\Dhushyanth\development\AppengineProjects\trunk_sanjhachoolha\sandbox\dushyanth\sanjhachoolha\views.py" in respond
  115. params['sign_in'] = users.create_login_url(request.path)

  AttributeError at /sanjhachoolha/acton/add
  'set' object has no attribute 'path'
Run Code Online (Sandbox Code Playgroud)

评论出来:

#value = list(set(re.split('^\s*|\s*,+\s*|\s*$', value)))
Run Code Online (Sandbox Code Playgroud)

我在相应的网页中获得了所需的输出:

r.dushaynth@gmail.com, dash@ben,, , abc@ac.com.edu
Run Code Online (Sandbox Code Playgroud)

我确信list()是我烦恼的根源.有人建议为什么会这样.还请建议替代方案.目的是从列表中删除重复项.

谢谢

uol*_*lot 8

好像你实现了自己的list()函数.它的return语句应该在你的文件的第208行(views.py).您应该将您的list()功能重命名为其他内容(偶数list_()).

编辑:你也可以改变你的regexp,像这样:

import re
value='   r.dushaynth@gmail.com, dash@ben,,  , abc@ac.com.edu    '
value = value.lower()

#value = list(set(re.split('^\s*|\s*,+\s*|\s*$', value)))
#if (value[0] == ''):
#    value.remove('')

value = set(re.findall(r'[\w\d\.\-_]+@[\w\d\.\-_]+', value))

print value
Run Code Online (Sandbox Code Playgroud)

re.findall()返回list所有匹配的出现次数.