我正在尝试将django id存储到列表中。但它将两位数字存储为单独的两位数字。例如,它将10存储为1和0.它将值存储为unicode字符。任何人都可以帮助我
def practice_view(request, id=None):
msg = ''
if request.method == 'POST':
questions = request.POST.get('Que');
list = [];
result = [];
total = 0;
for i in questions:
if i.isdigit():
list.append(i)
total = total + 1;
During debugging this is what it displays
Que=u'[6, 7, 8, 9, 10, 11, 12]'
questions=u'[6, 7, 8, 9, 10, 11, 12]'
list=[u'6', u'7', u'8', u'9', u'1', u'0', u'1', u'1', u'1', u'2']
答案 0 :(得分:0)
像这样更改你的代码,
import ast
def practice_view(request, id=None):
msg = ''
if request.method == 'POST':
questions = ast.literal_eval(request.POST.get('Que'))
lst = [];
result = [];
total = 0;
for i in questions:
if type(i) == 'int':
lst.append(i)
total = total + 1;
由于查询字符串param Que
的值是一个字符串,python应该迭代字符串中存在的每个字符。