Hướng dẫn python handle emoji - biểu tượng cảm xúc xử lý python

Vì vậy, tôi sẽ giả sử rằng những gì bạn bằng cách nào đó có được một chuỗi ASCII thô có chứa các chuỗi thoát với các đơn vị mã UTF-16 tạo thành các cặp thay thế và bạn (vì bất kỳ lý do gì) muốn chuyển đổi nó thành ____ ____ 7 định dạng.

Nội dung chính ShowShow

  • Làm thế nào để bạn sử dụng biểu tượng cảm xúc trong mã Python?
  • Mã hóa nào được sử dụng cho biểu tượng cảm xúc?
  • Làm cách nào để chuyển đổi biểu tượng cảm xúc thành văn bản bằng Python?
  • Python có thể đọc biểu tượng cảm xúc không?

Vì vậy, từ đó tôi cho rằng đầu vào của bạn (byte!) Trông như thế này:

weirdInput = "hello \\ud83d\\ude04".encode("latin_1")

Bây giờ bạn muốn làm như sau:

  1. Giải thích các byte theo cách mà các thứ
    smiley = (weirdInput
      .decode("raw_unicode_escape")
      .encode('utf-16', 'surrogatepass')
      .decode('utf-16')
    )
    
    6 được chuyển thành các đơn vị mã UTF-16. Có
    smiley = (weirdInput
      .decode("raw_unicode_escape")
      .encode('utf-16', 'surrogatepass')
      .decode('utf-16')
    )
    
    7, nhưng thật không may, nó cần một đường chuyền riêng để sửa các cặp thay thế (tôi không biết tại sao, thành thật mà nói)
  2. Sửa các cặp thay thế, chuyển đổi dữ liệu thành UTF-16 hợp lệ
  3. Giải mã là UTF-16 hợp lệ
  4. Một lần nữa, mã hóa là "RAW_UNICODE_ESCAPE"
  5. Giải mã trở lại là ____10 cũ tốt, chỉ bao gồm ascii cũ tốt với các chuỗi thoát Unicode ở định dạng
    smiley = (weirdInput
      .decode("raw_unicode_escape")
      .encode('utf-16', 'surrogatepass')
      .decode('utf-16')
    )
    
    8.

Một cái gì đó như thế này:

  output = (weirdInput
    .decode("raw_unicode_escape")
    .encode('utf-16', 'surrogatepass')
    .decode('utf-16')
    .encode("raw_unicode_escape")
    .decode("latin_1")
  )

Bây giờ nếu bạn

  output = (weirdInput
    .decode("raw_unicode_escape")
    .encode('utf-16', 'surrogatepass')
    .decode('utf-16')
    .encode("raw_unicode_escape")
    .decode("latin_1")
  )
2, bạn sẽ nhận được:
hello \U0001f604

Lưu ý rằng nếu bạn dừng ở giai đoạn trung gian:

smiley = (weirdInput
  .decode("raw_unicode_escape")
  .encode('utf-16', 'surrogatepass')
  .decode('utf-16')
)

Sau đó, bạn nhận được một chuỗi unicode với các mặt cười:

print(smiley)
# hello 😄

Mã đầy đủ:

weirdInput = "hello \\ud83d\\ude04".encode("latin_1")

output = (weirdInput
  .decode("raw_unicode_escape")
  .encode('utf-16', 'surrogatepass')
  .decode('utf-16')
  .encode("raw_unicode_escape")
  .decode("latin_1")
)


smiley = (weirdInput
  .decode("raw_unicode_escape")
  .encode('utf-16', 'surrogatepass')
  .decode('utf-16')
)

print(output)
# hello \U0001f604

print(smiley)
# hello 😄

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    Using Unicodes: 
    Every emoji has a Unicode associated with it. Emojis also have a CLDR short name, which can also be used. 
    From the list of unicodes, replace “+” with “000”. For example – “U+1F600” will become “U0001F600” and prefix the unicode with “\” and print it.
     

    Python3

      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    4
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    5
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    6
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    4
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    9
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    6
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    4
    hello \U0001f604
    
    3
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    6

    Bàn luận 
     

    Hướng dẫn python handle emoji - biểu tượng cảm xúc xử lý python

    ĐọcUsing Unicodes: Every emoji has a Unicode associated with it. Emojis also have a CLDR short name, which can also be used. From the list of unicodes, replace “+” with “000”. For example – “U+1F600” will become “U0001F600” and prefix the unicode with “\” and print it. 
    Using CLDR short name: 
     

    Python3

      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    4
    hello \U0001f604
    
    7
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    6
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    4
    smiley = (weirdInput
      .decode("raw_unicode_escape")
      .encode('utf-16', 'surrogatepass')
      .decode('utf-16')
    )
    
    1
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    6
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    4
    smiley = (weirdInput
      .decode("raw_unicode_escape")
      .encode('utf-16', 'surrogatepass')
      .decode('utf-16')
    )
    
    5
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    6

    Output:     
     

    Bàn luận  
    Using emoji module: 
    Emojis can also be implemented by using the emoji module provided in Python. To install it run the following in the terminal.
     

    hello \U0001f604
    
    1

    Có nhiều cách chúng ta có thể in biểu tượng cảm xúc trong Python. Hãy cùng xem cách in biểu tượng cảm xúc với unicodes, tên CLDR và ​​mô -đun biểu tượng cảm xúc. & Nbsp; sử dụng unicodes: & nbsp; mỗi biểu tượng cảm xúc đều có một unicode liên quan đến nó. Biểu tượng cảm xúc cũng có tên ngắn CLDR, cũng có thể được sử dụng. Ví dụ:, U+1F600, sẽ trở thành U0001F600 và tiền tố Unicode với \ \ và in nó. & Nbsp;Using CLDR short name:  
     

    Python3

    Đầu ra: & nbsp; & nbsp;Using emoji module: Emojis can also be implemented by using the emoji module provided in Python. To install it run the following in the terminal. 

      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
    print(smiley)
    # hello 😄
    
    0
    print(smiley)
    # hello 😄
    
    1
    print(smiley)
    # hello 😄
    
    2

    & nbsp; & nbsp; sử dụng tên ngắn CLDR: & nbsp; & nbsp; 

      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
    print(smiley)
    # hello 😄
    
    0
    print(smiley)
    # hello 😄
    
    9
    print(smiley)
    # hello 😄
    
    2

    Output:     
     

    & nbsp; & nbsp; sử dụng mô -đun Emoji: & nbsp; biểu tượng cảm xúc cũng có thể được thực hiện bằng cách sử dụng mô -đun Emoji được cung cấp trong Python. Để cài đặt nó chạy như sau trong thiết bị đầu cuối. & NBSP;
    demojize() function converts the emoji passed into its corresponding CLDR short name.  

    Hàm Emojize () yêu cầu tên ngắn CLDR được truyền trong đó dưới dạng tham số. Sau đó, nó trả về biểu tượng cảm xúc tương ứng. Thay thế các không gian bằng dấu gạch dưới trong tên ngắn CLDR. & Nbsp;
    Below is a list of some common emoji Unicodes with their CLDR short names:
    smiley = (weirdInput
      .decode("raw_unicode_escape")
      .encode('utf-16', 'surrogatepass')
      .decode('utf-16')
    )
    
    7
    smiley = (weirdInput
      .decode("raw_unicode_escape")
      .encode('utf-16', 'surrogatepass')
      .decode('utf-16')
    )
    
    8demojize() function converts the emoji passed into its corresponding CLDR short name.  
      output = (weirdInput
        .decode("raw_unicode_escape")
        .encode('utf-16', 'surrogatepass')
        .decode('utf-16')
        .encode("raw_unicode_escape")
        .decode("latin_1")
      )
    
    3
    print(smiley)
    # hello 😄
    
    0
    print(smiley)
    # hello 😄
    
    5
    print(smiley)
    # hello 😄
    
    2Below is a list of some common emoji Unicodes with their CLDR short names:
    & nbsp; & nbsp; demojize () Hàm chuyển đổi biểu tượng cảm xúc được truyền vào tên ngắn CLDR tương ứng của nó. & nbsp; & nbsp;
     
    & nbsp; & nbsp; Dưới đây là danh sách một số mô hình biểu tượng cảm xúc phổ biến với tên ngắn CLDR của họ:
    Tên ngắn CLDR 
     
    Unicode
    Mặt cười & NBSP; & nbsp; 
     
    U+1F600
    khuôn mặt cười toe toét với đôi mắt to & nbsp; & nbsp; 
     
    U+1F603
    khuôn mặt cười toe toét với đôi mắt mỉm cười & nbsp; & nbsp; 
     
    U+1F604
    khuôn mặt rạng rỡ với đôi mắt mỉm cười & nbsp; & nbsp; 
     
    U+1F601
    mặt cười nheo mắt & nbsp; & nbsp; 
     
    U+1F606
    khuôn mặt cười toe toét với mồ hôi & nbsp; & nbsp; 
     
    U+1F605
    lăn trên sàn cười & nbsp; & nbsp; 
     
    U+1F923
    khuôn mặt với những giọt nước mắt của niềm vui & nbsp; & nbsp; 
     
    U+1F602
    khuôn mặt hơi mỉm cười & nbsp; & nbsp;  U+1F642
    mặt lộn ngược & nbsp; & nbsp;
     
    U+1F643
    Khấp việc mặt 
     
    U+1F609
    khuôn mặt mỉm cười với đôi mắt mỉm cười & nbsp; & nbsp;  U+1F60A
    khuôn mặt mỉm cười với Halo & nbsp; & nbsp; U+1F607
    Khuôn mặt mỉm cười với 3 trái tim
     
    U+1F970
    khuôn mặt cười với đôi mắt trái tim 
     
    U+1F929
    khuôn mặt thổi một nụ hôn & nbsp; & nbsp; 
     
    U+1F618
    khuôn mặt hôn & nbsp; & nbsp; 
     
    U+1F617
    mặt cười & nbsp; & nbsp; 
     
    U+263a
    hôn mặt với đôi mắt nhắm & nbsp; & nbsp; 
     
    U+1f61a
    hôn mặt với đôi mắt mỉm cười & nbsp; & nbsp; 
     
    U+1F619
    mặt thưởng thức thực phẩm & nbsp; & nbsp; 
     
    U+1F60B
    mặt với lưỡi & nbsp; & nbsp; 
     
    U+1F61B
    Khai báo mặt với lưỡi & nbsp; & nbsp; 
     
    U+1F61C
    Zany Face & nbsp; & nbsp; 
     
    U+1F92a
    mặt nheo mắt với lưỡi & nbsp; & nbsp; 
     
    U+1f61d
    mặt truyền miệng & nbsp; & nbsp; 
     
    U+1F911
    ôm mặt & nbsp; & nbsp; 
     
    U+1F917
    mặt với bàn tay trên miệng & nbsp; & nbsp; 
     
    U+1f92d
    khuôn mặt lắc lư & nbsp; & nbsp; 
     
    U+1F92b
    Suy nghĩ khuôn mặt & nbsp; & nbsp; 
     
    U+1F914
    Khuôn mặt-mouth-mouth & nbsp; & nbsp; 
     
    U+1F910
    khuôn mặt với lông mày nhướn lên & nbsp; & nbsp; 
     
    U+1F928
    khuôn mặt trung tính & nbsp; & nbsp; 
     
    U+1F610
    khuôn mặt vô cảm & nbsp; & nbsp; 
     
    U+1F611
    mặt không có miệng & nbsp; & nbsp; 
     
    U+1F636
    mặt cười & nbsp; & nbsp; 
     
    U+263a
    hôn mặt với đôi mắt nhắm & nbsp; & nbsp; 
     
    U+1f61a
    hôn mặt với đôi mắt mỉm cười & nbsp; & nbsp; 
     
    U+1F619
    mặt thưởng thức thực phẩm & nbsp; & nbsp; 
     
    U+1F60B
    mặt với lưỡi & nbsp; & nbsp; 
     
    U+1F61B
    Khai báo mặt với lưỡi & nbsp; & nbsp; 
     
    U+1F61C
    Zany Face & nbsp; & nbsp; 
     
    U+1F92a
    mặt nheo mắt với lưỡi & nbsp; & nbsp; 
     
    U+1f61d
    mặt truyền miệng & nbsp; & nbsp; 
     
    U+1F911
    ôm mặt & nbsp; & nbsp; 
     
    U+1F917
    mặt với bàn tay trên miệng & nbsp; & nbsp; 
     
    U+1f92d
    khuôn mặt lắc lư & nbsp; & nbsp; 
     
    U+1F92b
    Suy nghĩ khuôn mặt & nbsp; & nbsp; 
     
    U+1F914

    Khuôn mặt-mouth-mouth & nbsp; & nbsp; 

    U+1F910 using the emoji module provided in Python. To install it run the following in the terminal. emojize() function requires the CLDR short name to be passed in it as the parameter. It then returns the corresponding emoji.

    khuôn mặt với lông mày nhướn lên & nbsp; & nbsp; 

    U+1F928 UTF-8 (Unicode) character set. UTF-8 covers almost all of the characters and symbols in the world.

    khuôn mặt trung tính & nbsp; & nbsp; 

    U+1F610 as the emoji list itself is frequently updated and changed. The below code block should be used for the above-mentioned download: Python3.

    khuôn mặt vô cảm & nbsp; & nbsp; 

    U+1F611 — not a feature developers would give priority to. However, a library called pythonji enables a user to do so. Users can not only use an emoji as a variable name but it can also be used as an alias in the import statement.