1

I am trying to write a program that will get tweets and then insert them into a csv file but I get this error:

Traceback (most recent call last):
  File "c:/Users/Fateh Aliyev/Desktop/Python/AI/Data Mining/data.py", line 30, in <module>
    csv.writerow([text, 0])
  File "C:\Python\lib\encodings\cp1252.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\U0001f44c' in position 41: character maps to <undefined>

I am sure that this is from the emojis that are in the strings. I tried this solution but I got the same error. Is this caused by python not being able to encode the string in the first place or something else? How do we get rid of the emojis?

Fateh
  • 302
  • 2
  • 12
  • You could use UTF-8 encoding for your file to avoid this problem (though you would need to remember to always specify UTF-8 as the encoding when opening the file. – snakecharmerb Apr 26 '20 at 15:07

1 Answers1

1

You can remove the emoji by ignoring it when it cannot be encoded:

import codecs

codecs.charmap_encode('\U0001f44c', 'ignore') 

# outputs: (b'', 1)
Richard
  • 215
  • 1
  • 9