Python - Поиск ручек Twitter - PullRequest
0 голосов
/ 28 января 2020

Я новичок в Python, но пытаюсь вытащить все маркеры Twitter из списка b ios. По сути, мне нужно вытащить то, что следует после'@'.

Пример ввода: ['Hello @ handle1', '@ handle2 @ handle3 hello', 'words3', '@ handle4']

Вывод: ['handle1', 'handle2', 'handle3', 'handle4']

Заранее спасибо!

Ответы [ 2 ]

0 голосов
/ 28 января 2020

Вы можете сделать это без регулярных выражений или импорта модулей:

text = ['Hello @handle1', '@handle2 @handle3 hello', 'words3', '@handle4']

handles = [word[1:] for word_group in text for word in word_group.split() if word.startswith('@')]
0 голосов
/ 28 января 2020

Регулярные выражения - ваш друг.

import re
import itertools

bios = ['Hello @handle1', '@handle2 @handle3 hello', 'words3', '@handle4']
handles = itertools.chain.from_iterable(re.findall(r"@(\w+)", bio) for bio in bios)

Здесь много чего происходит, поэтому давайте немного развернем его:

all_handles = []
for bio in bios:
    # bio = 'Hello @handle1', or '@handle2 @handle3 hello'
    this_bio_handles = re.findall(r"""
@                      # a literal "@" sign
(                      # beginning of capture group
    \w+                # one or more "Word" characters (a-z, 0-9, and space)
)                      # end of capture group""", bio, flags=re.X)
    # this_bio_handles = ['handle1'], or ['handle2', 'handle3']
    all_handles.append(this_bio_handles)

# all_handles = [['handle1'], ['handle2', 'handle3'], ['handle4']]
handles = itertools.chain.from_iterable(all_handles)
# itertools.chain produces an iterator from an iterable.
# itertools.chain.from_iterable produces an iterator from an iterable of iterables
# so handles ends up looking like 'handle1' 'handle2' 'handle3' 'handle4'
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...