1

I wrote a python program to obtain a string, and found there are images in some string, for example: , or "Siempre en día de la Madre la pasábamos así todos en familia dando mucho cariño a nuestra preciosa madre pero hoy la vamos a pasar solos extrañando a mamá pero siempre llevándola en nuestros corazones❤".

I want to delete theses images from the strings, obtaining only numbers and letters.

And please notice: these string are not only written in English, they may be written in all kinds of languages (for example: Arabic, or Japanese).

My program:

    for post_item in group_member_posts_list:
        if post_item['post_content']:
            post_item_content_str = post_item['post_content']
            print("post_item_content_str:" + post_item_content_str)
            post_item_content_str = filter(str.isalnum,post_item_content_str)
            print("after filter post_item_content_str:" + post_item_content_str )
            b = TextBlob(post_item_content_str)
            post_item_content_type = b.detect_language()

I tried to use filter function, but it gives errors. And isalnum function can only find English letters.

Could you please tell me how to resolve this problem?

lukess
  • 964
  • 1
  • 14
  • 19
bin
  • 51
  • 2
  • 7

1 Answers1

1

By image, I believe you meant emojis (), you can simply use re.sub to replace them from your string.

import re
emoji_finder = re.compile('[\U0001F300-\U0001F64F\U0001F680-\U0001F6FF\u2600-\u26FF\u2700-\u27BF]+')

tcase_1 =  "Siempre en día de la Madre la pasábamos así todos en familia dando mucho cariño a nuestra preciosa madre pero hoy la vamos a pasar solos extrañando a mamá pero siempre llevándola en nuestros corazones❤"

tcase_2 = "between"

print(re.sub(emoji_finder, "", tcase_1))
print(re.sub(emoji_finder, "", tcase_2))

Output:

Siempre en día de la Madre la pasábamos así 
todos en familia dando mucho cariño a nuestra 
preciosa madre pero hoy la vamos a pasar 
solos extrañando a mamá pero siempre 
llevándola en nuestros corazones

# and

between

Test it here: https://repl.it/IIWG

Adapted from this post and modified to support python 3.

Community
  • 1
  • 1
Taku
  • 31,927
  • 11
  • 74
  • 85