如何摆脱某些字符然后在python中输出一个清理过的字符串?

时间:2016-10-27 02:16:30

标签: python string groupme

在这段代码中,我试图获取某个用户在群聊中发布的图片的链接:

import groupy
from groupy import Bot, Group, Member

prog_group = Group.list().first
prog_members = prog_group.members()
prog_messages = prog_group.messages()
rojer = str(prog_members[4])
rojer_messages = ['none']
rojer_pics = []
links = open('rojer_pics.txt', 'w')

print(prog_group)

for message in prog_messages:
    if message.name == rojer:
        rojer_messages.append(message)
        if message.attachments:
            links.write(str(message) + '\n')

links.close()

问题是在链接文件中它打印整个消息:(" Rojer Doewns:Heres a special one + https://i.groupme.com/406x1199.png.7679b4f1ee964656bde93448ff9cee12')&gt ;" 我想要做的是摆脱不属于URL的字符,这样就像这样编写:

" https://i.groupme.com/406x1199.png.7679b4f1ee964656bde93448ff9cee12"

python中有没有可以像这样操作字符串的方法?

3 个答案:

答案 0 :(得分:0)

我刚使用string.split()并用括号分成3部分:

$questions = $this
    ->QuizDetails->find()
    ->contain([
        'Questions' => function($q) {
            return $q->contain(['Answers']);
        }
    ])
    ->where(['QuizDetails.quiz_id' => $id]);

答案 1 :(得分:0)

这可以使用字符串索引和字符串方法.find()完成:

>>> url = "(\"Rojer Doewns: Heres a special one +https://i.groupme.com/406x1199.png.7679b4f1ee964656bde93448ff9cee12')"
>>> url = url[url.find('+')+1:-2]
>>> url
'https://i.groupme.com/406x1199.png.7679b4f1ee964656bde93448ff9cee12'
>>> 

答案 2 :(得分:0)

>>> string = '("Rojer Doewns: Heres a special one +https://i.groupme.com/406x1199.png.7679b4f1ee964656bde93448ff9cee12\')>"'
>>> string.split('+')[1][:-4]
'https://i.groupme.com/406x1199.png.7679b4f1ee964656bde93448ff9cee12'