Python實現隨機生成一個漢字的方法分享
需求來源
在編寫爬蟲訓練場 項目時,碰到一個隨機頭像的需求,這里用漢字去隨機生成。
模擬的效果如下所示,輸入一組漢字,然后返回一張圖片。
接口地址如下所示:
https://ui-avatars.com/api/?name=夢想橡皮擦&background=03a9f4&color=ffffff&rounded=true
其中參數說明如下:
- name:待生成的文字內容;
- background:背景色;
- color:前景色;
- rounded:是否圓形。
我們在下一篇博客完成生成圖片效果,本篇先實現隨機漢字生成。
隨機漢字
生成隨機漢字的模塊不是 Python 自帶的功能,但是你可以使用 Python 的 random 模塊來生成隨機數,然后使用 Unicode 編碼來獲取對應的漢字。
下面是一個簡單的例子,它生成一個隨機的漢字:
import random
def get_random_char():
# 漢字編碼的范圍是0x4e00 ~ 0x9fa5
val = random.randint(0x4e00, 0x9fa5)
# 轉換為Unicode編碼
return chr(val)
print(get_random_char())
如果你想生成多個隨機漢字,可以使用一個循環(huán)來調用 get_random_char() 函數,并將生成的漢字拼接起來。
下面的代碼生成了 5 個隨機漢字:
import random
def get_random_char():
# 漢字編碼的范圍是0x4e00 ~ 0x9fa5
val = random.randint(0x4e00, 0x9fa5)
# 轉換為Unicode編碼
return chr(val)
# 生成5個隨機漢字
random_chars = ""
for i in range(5):
random_chars += get_random_char()
print(random_chars)
隨機生成常用漢字
直接使用 Unicode 編碼,會出現很生僻字,在實戰(zhàn)中可以使用部分策略解決該問題,例如找一篇長文,將其存儲到一個文本文件中,然后使用 Python 的讀寫文件功能來讀取文件中的漢字。
從互聯網找一段文字,添加到 demo.txt 中,用于后續(xù)生成隨機漢字。
從 demo.txt 中讀取文字,這里再補充一個步驟,由于隨機生成的文本中會有標點符號,所以需要進行去除。使用 Python 的字符串方法 translate 來實現。
import string
s = "Hello, xiangpica! How are you today?"
# 創(chuàng)建字符映射表
translator = str.maketrans('', '', string.punctuation)
# 使用字符映射表去除標點符號
s = s.translate(translator)
print(s)
結果該方法僅能去除 ASCII 編碼的標點符號(例如 !、? 等)。如果去除 Unicode 編碼的標點符號,還需要切換方法。
最終選定的模塊是 Python 的 unicodedata 模塊,其提供了一系列的函數,可以幫助你處理 Unicode 字符的相關信息。
下面是一些常用的 unicodedata 函數:
import unicodedata
print(unicodedata.name('X'))
print(unicodedata.name('0'))
print(unicodedata.name('@'))
unicodedata.lookup(name):返回給定名稱的 Unicode 字符。
import unicodedata
print(unicodedata.lookup('LATIN CAPITAL LETTER X')) # X
print(unicodedata.lookup('DIGIT ZERO')) # 0
print(unicodedata.lookup('COMMERCIAL AT')) # @
可以使用 unicodedata.category() 函數來判斷一個字符是否是標點符號,然后再使用 translate() 函數來去除標點符號。
下面這段代碼,就是使用了 unicodedata 模塊和 translate() 函數來去除一段文本中的所有標點符號:
import unicodedata
s = "Hello, xiangpica! How are you today?"
# 創(chuàng)建字符映射表
translator = {ord(c): None for c in s if unicodedata.category(c).startswith('P')}
# 使用字符映射表去除標點符號
s = s.translate(translator)
print(s)
將上述代碼集成到 Python 隨機生成漢字中,示例如下。
import random
import unicodedata
def get_random_common_char():
# 讀取文件中的常用漢字
with open('demo.txt', 'r',encoding='utf-8') as f:
common_chars = f.read()
# 去除空格
common_chars = common_chars.replace(' ','')
common_chars = common_chars.strip()
# 創(chuàng)建字符映射表
translator = {ord(c): None for c in common_chars if unicodedata.category(c).startswith('P')}
# 使用字符映射表去除標點符號
s = common_chars.translate(translator)
return random.choice(s)
print(get_random_common_char())
隨機生成五個漢字
random_chars = ""
for i in range(5):
random_chars += get_random_common_char()
print(random_chars)到此這篇關于Python實現隨機生成一個漢字的方法分享的文章就介紹到這了,更多相關Python隨機生成漢字內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
jupyter?notebook?自定義python解釋器的過程詳解
大家都知道jupyter?notebook?網頁版交互環(huán)境,類似于ipython,功能強大,這篇文章主要介紹了jupyter?notebook?自定義python解釋器的過程,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-10-10
Python socket套接字實現C/S模式遠程命令執(zhí)行功能案例
這篇文章主要介紹了Python socket套接字實現C/S模式遠程命令執(zhí)行功能,涉及Python socket套接字編寫服務器/客戶機模式數據傳輸相關操作技巧,需要的朋友可以參考下2018-07-07
Python使用sqlite3第三方庫讀寫SQLite數據庫的方法步驟
數據庫非常重要,程序的數據增刪改查需要數據庫支持,python處理數據庫非常簡單,而且不同類型的數據庫處理邏輯方式大同小異,下面這篇文章主要給大家介紹了關于Python使用sqlite3第三方庫讀寫SQLite數據庫的方法步驟,需要的朋友可以參考下2022-07-07
使用Python中PDB模塊中的命令來調試Python代碼的教程
這篇文章主要介紹了使用Python中PDB模塊中的命令來調試Python代碼的教程,包括設置斷點來修改代碼等、對于Python團隊項目工作有一定幫助,需要的朋友可以參考下2015-03-03

