最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

【已解决】Python中分隔字符串为列表

Python crifan 1986浏览 0评论

python 字符串 开头

eventKey=JOIN_EVENT_1

判断字符串是否以

JOIN_EVENT_

开头

Python startswith() 函数 判断字符串开头 – doiido的专栏 – 博客频道 – CSDN.NET

String操作篇-python – 鱼骨头 – 51CTO技术博客

2.2 字符串开头或结尾匹配 — python3-cookbook 2.0.0 文档

此处可以用:

eventKey.startswith(“JOIN_EVENT_”)

已有字符串

eventKey=JOIN_EVENT_1

需要去:

分隔为list

获取对应的1

Split string into a list in Python – Stack Overflow

Python String split() Method

Python split()方法 | 菜鸟教程

此处可以用

eventKey.split(“_”)

去得到对应的list

不过

后来改用正则表达式从字符串中提取对应内容,即:

对于字符串

eventKey=JOIN_EVENT_1

想要通过正则去判断字符串是否以:

JOIN_EVENT_

开头

然后再提取对应的最后的数字

Python正则表达式 | 菜鸟教程

Python正则表达式指南 – AstralWind – 博客园

Python正则表达式操作指南 – Ubuntu中文

正则表达式 – 廖雪峰的官方网站

然后通过如下代码即可得到结果:

# eventKey=JOIN_EVENT_1
                    joinEventMatch = re.match(r’^JOIN_EVENT_(?P<eventId>\d+)$’, eventKey)
                    app.logger.debug(“joinEventMatch=%s”, joinEventMatch)
                    # joinEventMatch=<_sre.SRE_Match object at 0x7f5a2b8c6990>
                    if joinEventMatch:
                        eventId = joinEventMatch.group(“eventId”)
                        app.logger.debug(“eventId=%s”, eventId) #eventId=1
                        eventIdInt = int(eventId)
                        app.logger.debug(“eventIdInt=%s”, eventIdInt) #eventIdInt=1

转载请注明:在路上 » 【已解决】Python中分隔字符串为列表

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.166 seconds, using 22.05MB memory