Printing unicode number of chars in a string (Python)

2024/9/26 1:16:08

This should be simple, but I can't crack it.

I have a string of Arabic symbols between u'\u0600' - u'\u06FF' and u'\uFB50' - u'\uFEFF'. For example غينيا واستمر العصبة ضرب قد.

How do I print each character's unicode number? I'm using Python 2.7.


Something like the following gives me decoding Unicode is not supported:

for c in example_string:print unicode(c,'utf-8')
Answer

You can use the ord() function.

for c in example_string:print(ord(c), hex(ord(c)), c.encode('utf-8'))

will give you the decimal, hex codepoint as well as the UTF-8 encoding for this character, like so:

(1594, '0x63a', '\xd8\xba')
(1610, '0x64a', '\xd9\x8a')
(1606, '0x646', '\xd9\x86')
(1610, '0x64a', '\xd9\x8a')
(1575, '0x627', '\xd8\xa7')
(32, '0x20', ' ')::
https://en.xdnf.cn/q/71511.html

Related Q&A

Pandas report top-n in group and pivot

I am trying to summarise a dataframe by grouping along a single dimension d1 and reporting summary statistics for each element of d1. In particular I am interested in the top n (index and values) for …

virtualenv --no-site-packages is not working for me

virtualenv --no-site-packages v1cd v1\Scriptsactivate.batpython -c "import django" # - no problem hereWhy does it see the Django package??? It should give me an import error, right?

pandas: Group by splitting string value in all rows (a column) and aggregation function

If i have dataset like this:id person_name salary 0 [alexander, william, smith] 45000 1 [smith, robert, gates] 65000 2 [bob, alexander] …

Seaborn Title Position

The position of my graph title is terrible on this jointplot. Ive tried moving the loc = left, right, and center but it doesnt move from the position its in. Ive also tried something like ax.title.set_…

Expand/collapse ttk Treeview branch

I would like to know the command for collapsing and expanding a branch in ttk.Treeview.Here is a minimalistic example code:#! coding=utf-8 import tkinter as tk from tkinter import ttkroot = tk.Tk() tre…

Uploading images to s3 with meta = image/jpeg - python/boto3

How do I go about setting ContentType on images that I upload to AWS S3 using boto3 to content-type:image/jpeg?Currently, I upload images to S3 using buto3/python 2.7 using the following command:s3.up…

How to use win environment variable pathlib to save files?

Im trying to use win environment variable like %userprofile%\desktop with pathlib to safe files in different users PC.But Im not able to make it work, it keep saving in on the running script dir.import…

Difference between starting firestore emulator through `firebase` and `gcloud`?

What is the difference between starting the firestore emulator through: firebase emulators:start --only firestoreand: gcloud beta emulators firestore startBoth options allow my python app to achieve co…

PyInstaller icon option doesnt work on Mac

I ran the following command on my mac and created an .app file.pyinstaller --icon icon.icns --noconsole -n testApp main.pyHowever, the generated .app file does not show the icon.icon.icns is specified …

Error Installing scikit-learn

When trying to install scikit-learn, I get the following error:Exception:Traceback (most recent call last):File "/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2…