Parsing a tweet to extract hashtags into an array

前端 未结 9 757
旧时难觅i
旧时难觅i 2020-12-03 05:33

I am having a heck of a time taking the information in a tweet including hashtags, and pulling each hashtag into an array using Python. I am embarrassed to even put what I

9条回答
  •  甜味超标
    2020-12-03 06:07

    I extracted hashtags in a silly but effective way.

    def retrive(s):
        indice_t = []
        tags = []
        tmp_str = ''
        s = s.strip()
        for i in range(len(s)):
            if s[i] == "#":
                indice_t.append(i)
        for i in range(len(indice_t)):
            index = indice_t[i]
            if i == len(indice_t)-1:
                boundary = len(s)
            else:
                boundary = indice_t[i+1]
            index += 1
            while index < boundary:
                if s[index] in "`~!@#$%^&*()-_=+[]{}|\\:;'"",.<>?/ \n\t":
                    tags.append(tmp_str)
                    tmp_str = ''
                    break
                else:
                    tmp_str += s[index]
                    index += 1
            if tmp_str != '':
                tags.append(tmp_str)
        return tags
    

提交回复
热议问题