Python實(shí)現(xiàn)隨機(jī)生成一個漢字的方法分享
需求來源
在編寫爬蟲訓(xùn)練場 項目時,碰到一個隨機(jī)頭像的需求,這里用漢字去隨機(jī)生成。
模擬的效果如下所示,輸入一組漢字,然后返回一張圖片。
接口地址如下所示:
https://ui-avatars.com/api/?name=夢想橡皮擦&background=03a9f4&color=ffffff&rounded=true
其中參數(shù)說明如下:
- name:待生成的文字內(nèi)容;
- background:背景色;
- color:前景色;
- rounded:是否圓形。
我們在下一篇博客完成生成圖片效果,本篇先實(shí)現(xiàn)隨機(jī)漢字生成。
隨機(jī)漢字
生成隨機(jī)漢字的模塊不是 Python 自帶的功能,但是你可以使用 Python 的 random 模塊來生成隨機(jī)數(shù),然后使用 Unicode 編碼來獲取對應(yīng)的漢字。
下面是一個簡單的例子,它生成一個隨機(jī)的漢字:
import random
def get_random_char():
# 漢字編碼的范圍是0x4e00 ~ 0x9fa5
val = random.randint(0x4e00, 0x9fa5)
# 轉(zhuǎn)換為Unicode編碼
return chr(val)
print(get_random_char())
如果你想生成多個隨機(jī)漢字,可以使用一個循環(huán)來調(diào)用 get_random_char() 函數(shù),并將生成的漢字拼接起來。
下面的代碼生成了 5 個隨機(jī)漢字:
import random
def get_random_char():
# 漢字編碼的范圍是0x4e00 ~ 0x9fa5
val = random.randint(0x4e00, 0x9fa5)
# 轉(zhuǎn)換為Unicode編碼
return chr(val)
# 生成5個隨機(jī)漢字
random_chars = ""
for i in range(5):
random_chars += get_random_char()
print(random_chars)
隨機(jī)生成常用漢字
直接使用 Unicode 編碼,會出現(xiàn)很生僻字,在實(shí)戰(zhàn)中可以使用部分策略解決該問題,例如找一篇長文,將其存儲到一個文本文件中,然后使用 Python 的讀寫文件功能來讀取文件中的漢字。
從互聯(lián)網(wǎng)找一段文字,添加到 demo.txt 中,用于后續(xù)生成隨機(jī)漢字。
從 demo.txt 中讀取文字,這里再補(bǔ)充一個步驟,由于隨機(jī)生成的文本中會有標(biāo)點(diǎn)符號,所以需要進(jìn)行去除。使用 Python 的字符串方法 translate 來實(shí)現(xiàn)。
import string
s = "Hello, xiangpica! How are you today?"
# 創(chuàng)建字符映射表
translator = str.maketrans('', '', string.punctuation)
# 使用字符映射表去除標(biāo)點(diǎn)符號
s = s.translate(translator)
print(s)
結(jié)果該方法僅能去除 ASCII 編碼的標(biāo)點(diǎn)符號(例如 !、? 等)。如果去除 Unicode 編碼的標(biāo)點(diǎn)符號,還需要切換方法。
最終選定的模塊是 Python 的 unicodedata 模塊,其提供了一系列的函數(shù),可以幫助你處理 Unicode 字符的相關(guān)信息。
下面是一些常用的 unicodedata 函數(shù):
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() 函數(shù)來判斷一個字符是否是標(biāo)點(diǎn)符號,然后再使用 translate() 函數(shù)來去除標(biāo)點(diǎn)符號。
下面這段代碼,就是使用了 unicodedata 模塊和 translate() 函數(shù)來去除一段文本中的所有標(biāo)點(diǎn)符號:
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')}
# 使用字符映射表去除標(biāo)點(diǎn)符號
s = s.translate(translator)
print(s)
將上述代碼集成到 Python 隨機(jī)生成漢字中,示例如下。
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')}
# 使用字符映射表去除標(biāo)點(diǎn)符號
s = common_chars.translate(translator)
return random.choice(s)
print(get_random_common_char())
隨機(jī)生成五個漢字
random_chars = ""
for i in range(5):
random_chars += get_random_common_char()
print(random_chars)到此這篇關(guān)于Python實(shí)現(xiàn)隨機(jī)生成一個漢字的方法分享的文章就介紹到這了,更多相關(guān)Python隨機(jī)生成漢字內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python編程使用Selenium模擬淘寶登錄實(shí)現(xiàn)過程
這篇文章主要介紹了Python編程使用Selenium模擬淘寶登錄的實(shí)現(xiàn)過程示例及解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2021-10-10
Python實(shí)戰(zhàn)之ATM取款機(jī)的實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了如何利用Python語言模擬實(shí)現(xiàn)ATM取款機(jī)功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2022-09-09
Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例
今天小編就為大家分享一篇Python 僅獲取響應(yīng)頭, 不獲取實(shí)體的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-08-08
Windows下anaconda安裝第三方包的方法小結(jié)(tensorflow、gensim為例)
conda的設(shè)計理念——conda將幾乎所有的工具、第三方包都當(dāng)做package對待,甚至包括python和conda自身!因此,conda打破了包管理與環(huán)境管理的約束,能非常方便地安裝各種版本python、各種package并方便地切換2018-04-04

