检索存储在(?P <topic_id> \ d +)中的整数

结石

urls.pyDjango的

#urls.py
url(r'^topics/(?P<topic_id>\d+)/$', views.topic, name='topic')
The second part of the expression, /(?P<topic_id>\d+)/, matches an integer between two forward slashes and stores the integer value in an argument called topic_id.

我试图用正则表达式了解它

In [6]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/")
Out[6]: ['1']

但是,当我尝试

In [7]: re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/").topic_id
AttributeError: 'list' object has no attribute 'topic_id'

看来整数没有存储在中topic_id,如何理解?

ul

您的错误不是来自“ topic_id”,而是关于re

如果使用re.findall,它将返回与您的正则表达式匹配的所有列表。

因此,您的情况re.findall(r'topics/(?P<topic_id>\d+)/$', "topics/1/")将为['1']

因此,当然要['1'].topic_id提高AttributeError。

如果要分组'topic_id',请这样做

p = re.match(r'topics/(?P<topic_id>\d+)/$', "topics/1/")
p.group('topic_id') # it returns '1'

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

P在`/(?P <topic_id> \ d +)$`中的含义是什么

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException:“字段列表”中的未知列“ topic_id”

找不到带有参数“('',)”的“ new_entry”。尝试了1个模式:['new_entry /(?P <topic_id> [0-9] +)/ $']

使用Django在sqlite中存储和检索整数列表

如何从数据库中检索整数值并将其存储在.net中的整数中

使用mysql检索存储过程中的插入ID

MySQL从存储过程中检索last_insert_id

从数据存储区中的实体检索ID

在2D数组中存储字符串和整数

如何从CMIS存储库中检索“存储库根” ID /子代?

如何从数据库中检索整数数据并将其存储在变量中?

在 Keychain Swift 中存储整数

在malloc的块中存储整数

整数如何存储在Excel中

Axon 框架:如何在单元测试中检索状态存储的聚合 ID 的 ID

从Firebase存储中检索图像

从Firebase存储中检索文件

检索存储在 mongodb 中的图像

在localStorage中存储和检索

如何从结果集中在数组列表中存储2个整数以及如何检索它

如何在SQL Server存储过程中检索整数列表?

无法存储和从Chrome存储中检索

从气流连接 ID 中检索 azure blob 存储连接字符串

无法在数据存储区中检索具有生成ID的实体

如何通过传递ID数组作为Google数据存储区中的输入来检索实体?

是否可以在整数指针(int *)中存储有符号整数?

Java中的BitSet是否存储位或整数?

让Spritekit显示存储在变量中的整数

App Engine中的整数类型存储