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.
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:
- Giải thích các byte theo cách mà các thứ
\uXXXX
được chuyển thành các đơn vị mã UTF-16. Córaw_unicode_escapes
, 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] - Sửa các cặp thay thế, chuyển đổi dữ liệu thành UTF-16 hợp lệ
- Giải mã là UTF-16 hợp lệ
- Một lần nữa, mã hóa là "RAW_UNICODE_ESCAPE"
- 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
\UXXXXXXXX
.
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
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"]
]
4hello \U0001f604
3 output = [weirdInput
.decode["raw_unicode_escape"]
.encode['utf-16', 'surrogatepass']
.decode['utf-16']
.encode["raw_unicode_escape"]
.decode["latin_1"]
]
6Bàn luận
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
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"]
]
4hello \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"]
]
4smiley = [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"]
]
4smiley = [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"]
]
6Output:
Đầ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.
pip install emoji
& nbsp; & nbsp; sử dụng tên ngắn CLDR: & nbsp; & nbsp;
Python3
& 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;
output = [weirdInput
.decode["raw_unicode_escape"]
.encode['utf-16', 'surrogatepass']
.decode['utf-16']
.encode["raw_unicode_escape"]
.decode["latin_1"]
]
3print[smiley]
# hello 😄
0print[smiley]
# hello 😄
1print[smiley]
# hello 😄
2Hà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;
output = [weirdInput
.decode["raw_unicode_escape"]
.encode['utf-16', 'surrogatepass']
.decode['utf-16']
.encode["raw_unicode_escape"]
.decode["latin_1"]
]
3print[smiley]
# hello 😄
0print[smiley]
# hello 😄
9print[smiley]
# hello 😄
2Output:
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"]
]
3print[smiley]
# hello 😄
0print[smiley]
# hello 😄
5print[smiley]
# hello 😄
2Below is a list of some common emoji Unicodes with their CLDR short names:
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+1F60D |
Star-Struck & nbsp; & nbsp; | 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+1F612 |
mặt với đôi mắt lăn & nbsp; & nbsp; | U+1F644 |
khuôn mặt nhăn nhó & nbsp; & nbsp; | U+1F62C |
nói dối khuôn mặt & nbsp; & nbsp; | U+1F925 |
Felieed Face & nbsp; & nbsp; | U+1F60C |
khuôn mặt trầm ngâm & nbsp; & nbsp; | U+1F614 |
khuôn mặt buồn ngủ & nbsp; & nbsp; | U+1f62a |
mặt chảy nước dãi & nbsp; & nbsp; | U+1F924 |
mặt ngủ & nbsp; & nbsp; | U+1F634 |
Đối mặt với mặt nạ y tế & nbsp; & nbsp; | U+1F637 |
mặt với nhiệt kế & nbsp; & nbsp; | U+1F912 |
Đối mặt với Băng tần đầu & NBSP; & NBSP; | U+1F915 |
mặt buồn nôn & nbsp; & nbsp; | U+1F922 |