[英]How To Replace Hashtags with HTML Anchor in Python/Django?
message = "@me this is nice. @you: let me #help you. #anyone> #Python, #All."
message.split()
給出“
['@me', 'this', 'is', 'nice.', '@you:', 'let', 'me', '#help', 'you.', '#anyone>', '#Python,', '#All.']
但是我想要的是
['@me', 'this', 'is', 'nice.', '@you', 'let', 'me', '#help', 'you.', '#anyone', '#Python', '#All']
。 沒有:
, .
, >
或其他任何符號。 我只想一個人說話 。
startswith('#')
應該返回
['#help', '#anyone', '#Python', #All]
然后hashtag_links
將返回
["<a href='hashtags\\help'>#help</a>", "<a href='hashtags\\anyone'>#anyone</a>", ...]
我想做的是能夠用hashtag_links
的等效標簽替換message
中的hashtag_links
標簽,以便它們在HTML中呈現時可以單擊。
您可以使用列表推導很容易地做到這一點:
mylist = [i.rstrip(":;") for i in message.split() if i] # remove blanks
hashtagged = [i for i in mylist if i.startswith("#")]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.