几个简单且有意思的python包

中文简繁体转换包 - zhconv

 简介:

支持中文简繁体之间的转换、识别

支持以下类别:

  zh-cn 大陆简体

  zh-tw 台灣正體

  zh-hk 香港繁體

  zh-sg 马新简体

  zh-hans 简体

  zh-hant 繁體

 安装:

pip install zhconv

 简单使用:

示例:

from zhconv import convert

print(convert(u'我幹什麼不干你事。','zh-cn'))  #我干什么不干你事。
print(convert(u'人体内存在很多微生物','zh-tw'))  #人體內存在很多微生物

完整支持 MediaWiki 人工转换语法:

from zhconv import convert_for_mw


print(convert_for_mw(u'在现代,机械计算-{}-机的应用已经完全被电子计算-{}-机所取代','zh-hk')) 
#在現代,機械計算機的應用已經完全被電子計算機所取代
print(convert_for_mw(u'-{zh-hant:資訊工程;zh-hans:计算机工程学;}-是电子工程的一个分支,主要研究计算机软硬件和二者间的彼此联系。','zh-tw')) 
#資訊工程是電子工程的一個分支,主要研究計算機軟硬體和二者間的彼此聯繫。
print(convert_for_mw(u'張國榮曾在英國-{zh:利兹;zh-hans:利兹;zh-hk:列斯;zh-tw:里茲}-大学學習。','zh-sg')) 
#张国荣曾在英国利兹大学学习。
print(convert_for_mw('毫米(毫公分),符號mm,是長度單位和降雨量單位,-{zh-hans:台湾作-{公釐}-或-{公厘}-;zh-hant:港澳和大陸稱為-{毫米}-(台灣亦有使用,但較常使用名稱為毫公分);zh-mo:台灣作-{公釐}-或-{公厘}-;zh-hk:台灣作-{公釐}-或-{公厘}-;}-。','zh-cn')) 
#毫米(毫公分),符号mm,是长度单位和降雨量单位,台湾作公釐或公厘。

参考链接:https://www.cnpython.com/pypi/zhconv

拼音包 - pypinyin

 简介:

获取汉子拼音

 安装:

pip install pypinyin

 简单使用;

from pypinyin import pinyin, Style

print(pinyin("上山打老虎")) # 默认
# [['shàng'], ['shān'], ['dǎ'], ['lǎo'], ['hǔ']]
print(pinyin("上山打老虎", style=Style.NORMAL)) # 普通风格
# [['shang'], ['shan'], ['da'], ['lao'], ['hu']]
print(pinyin("上山打老虎", style=Style.FIRST_LETTER)) # 首字母
# [['s'], ['s'], ['d'], ['l'], ['h']]

参考链接:

  1.https://blog.csdn.net/zhoulei124/article/details/89055403

  2.https://www.cnblogs.com/baby123/p/13409481.html

  3.https://www.cnpython.com/pypi/pypinyin

键盘鼠标监听包 - pynput

 简介:

  监听键盘/鼠标事件、操控键盘/鼠标

 安装:

pip install pynput

简单使用:

控制鼠标:

from pynput.mouse import Button, Controller

mouse = Controller()

# Read pointer position
print('The current pointer position is {0}'.format(
    mouse.position))

# Set pointer position
mouse.position = (10, 20)
print('Now we have moved it to {0}'.format(
    mouse.position))

# Move pointer relative to current position
mouse.move(5, -5)

# Press and release
mouse.press(Button.left)
mouse.release(Button.left)

# Double click; this is different from pressing and releasing
# twice on Mac OSX
mouse.click(Button.left, 2)

# Scroll two steps down
mouse.scroll(0, 2)

  

监听鼠标:

from pynput import mouse

def on_move(x, y):
    print('Pointer moved to {0}'.format(
        (x, y)))

def on_click(x, y, button, pressed):
    print('{0} at {1}'.format(
        'Pressed' if pressed else 'Released',
        (x, y)))
    if not pressed:
        # Stop listener
        return False

def on_scroll(x, y, dx, dy):
    print('Scrolled {0} at {1}'.format(
        'down' if dy < 0 else 'up',
        (x, y)))

# Collect events until released
with mouse.Listener(
        on_move=on_move,
        on_click=on_click,
        on_scroll=on_scroll) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = mouse.Listener(
    on_move=on_move,
    on_click=on_click,
    on_scroll=on_scroll)
listener.start()

 

监听键盘

from pynput import keyboard

def on_press(key):
    try:
        print('alphanumeric key {0} pressed'.format(
            key.char))
    except AttributeError:
        print('special key {0} pressed'.format(
            key))

def on_release(key):
    print('{0} released'.format(
        key))
    if key == keyboard.Key.esc:
        # Stop listener
        return False

# Collect events until released
with keyboard.Listener(
        on_press=on_press,
        on_release=on_release) as listener:
    listener.join()

# ...or, in a non-blocking fashion:
listener = mouse.Listener(
    on_press=on_press,
    on_release=on_release)
listener.start() 

控制键盘

from pynput.keyboard import Key, Controller

keyboard = Controller()

# Press and release space
keyboard.press(Key.space)
keyboard.release(Key.space)

# Type a lower case A; this will work even if no key on the
# physical keyboard is labelled 'A'
keyboard.press('a')
keyboard.release('a')

# Type two upper case As
keyboard.press('A')
keyboard.release('A')
with keyboard.pressed(Key.shift):
    keyboard.press('a')
    keyboard.release('a')

# Type 'Hello World' using the shortcut type method
keyboard.type('Hello World')

posted on 2021-07-23 16:39  懒得显示  阅读(291)  评论(0编辑  收藏  举报