Search code examples
pythonemojipython-unicode

Encoding Unicoded emoji from CSV file with text strings together


I have an issue with encoding of unicodes from CSV file. I have looked at SO , but does not give a proper solution. My CSV file is so,

Subject1,u"\U0001f3c6 Campaign for you"
Subject2,\U0001f3c6  Special Offer to you
Subject3,u'\U0001f3c6' UP to %25 Sales
Subject4,u\U0001f3c6 UP to %50 Sales

I am reading it into a dictionary. Then trying to encode to utf-8, not using open(encoding=utf-8) option delibaretely, somehow does not solve when using.

Here is code, where I had put all solutions to see in which case my code prints emoji, in none of them. I need to assist in my code

def emojis():
    file_ = file_reader.ReadFile().pathLooker('./Subjects/Subjects.csv')
    csv_ = open(file_,'r')
    data=dict(csv.reader(csv_,delimiter=','))
    print(data["Subject1"].encode('utf-8').decode('ascii'),type(data["Subject1"].encode('utf-8').decode('ascii')))
    print(data["Subject2"].encode('utf-8').decode('ascii'), type(data["Subject1"].encode('utf-8').decode('ascii')))
    print(data["Subject3"].encode('utf-8').decode('ascii'), type(data["Subject1"].encode('utf-8').decode('ascii')))
    print(data["Subject4"].encode('utf-8').decode('ascii'), type(data["Subject1"].encode('utf-8').decode('ascii')))
    print(data["Subject1"].encode('utf-8'),type(data["Subject1"].encode('utf-8')))
    print(data["Subject2"].encode('utf-8'), type(data["Subject1"].encode('utf-8')))
    print(data["Subject3"].encode('utf-8'), type(data["Subject1"].encode('utf-8')))
    print(data["Subject4"].encode('utf-8'), type(data["Subject1"].encode('utf-8')))
    emoji = '\U0001f3c6 hello how'

Solution

  • You can decode your string with escape characters using unicode_escape as the decoding.

    For example, the second line of your csv file can be decoded using the following line:

    data['Subject2'].encode().decode("unicode_escape")
    

    Output:

    🏆  Special Offer to you