list()在appengine中不起作用?

时间:2022-08-23 14:53:04

i am trying to use set function in appengine, to prepare a list with unique elements. I hit a snag when i wrote a python code which works fine in the python shell but not in appengine + django

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

This is what i intend to do(ran this script in IDLE):

这是我打算做的(在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       

The desired output is(got this output in IDLE):

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

['dash@ben', 'abc@ac.com.edu', 'r.dushaynth@gmail.com']

Now when i do something equivalent in my views.py file in appengine :

现在,当我在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,
    })

I get this error while going to the appropriate page(pasting the traceback):

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

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'

on commenting out:

评论出来:

#value = list(set(re.split('^\s*|\s*,+\s*|\s*$', value)))

I get the desired output in the appropriate webpage:

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

r.dushaynth@gmail.com, dash@ben,, , abc@ac.com.edu

I am sure the list() is the root of my troubles. CAn anyone suggest why this is happening. Please also suggest alternatives. The aim is to remove duplicates from the list.

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

Thanks

1 个解决方案

#1


It seems like you implemented your own list() function. Its return statements should be at line 208 of your file (views.py). You should rename your list() function to something else (even list_()).

看起来你实现了自己的list()函数。它的return语句应该在你的文件的第208行(views.py)。您应该将list()函数重命名为其他内容(甚至是list_())。

EDIT: Also you can change you regexp, like this:

编辑:你也可以改变你的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

re.findall() returns a list of all matched occurences.

re.findall()返回所有匹配的出现的列表。

#1


It seems like you implemented your own list() function. Its return statements should be at line 208 of your file (views.py). You should rename your list() function to something else (even list_()).

看起来你实现了自己的list()函数。它的return语句应该在你的文件的第208行(views.py)。您应该将list()函数重命名为其他内容(甚至是list_())。

EDIT: Also you can change you regexp, like this:

编辑:你也可以改变你的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

re.findall() returns a list of all matched occurences.

re.findall()返回所有匹配的出现的列表。