pythonctypes程序在3.2上运行,但在3.4上不兼容

2024-09-27 18:01:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个程序,标记泰语文本调用libtahic库。 这个程序在python3.2中运行正常,但在python3.4中失败。 你知道为什么3.4版会失败吗?你知道吗

请在下面找到两个版本的python的程序代码和输出:

#!/usr/bin/python3
# apt-get install libthai0 libthai-doc libthai-dev libthai-data
from ctypes import *
from ctypes.util import find_library

THAI_LIBRARY = find_library('thai')
if not THAI_LIBRARY:
    raise OSError('Cannot find libthai in the system')

thai = cdll.LoadLibrary(THAI_LIBRARY)

thai.th_wbrk_line.restype = c_int
thai.th_wbrk_line.argtypes = [c_wchar_p,c_wchar_p,c_size_t,c_wchar_p]

def whitespace(ain):
    # expects bytes
    ain=ain.decode('utf8')
    aout=whitespace_string(ain)
    return aout.encode('utf8')

def whitespace_string(ain):
    # expects a string
    # ain='แล้วพบกันใหม่'
    aout=' '*(2*len(ain)+1) 
    # we assume that the maximum length is a set of one character + white space
    adelim=' '
    asize=len(aout)
    res=thai.th_wbrk_line(ain,aout,asize,adelim)
    result=aout[:res]
    return result

"""
แล้วพบกันใหม่ means 'See you later.'

and is compound by three words:

แล้ว
พบกัน
ใหม่
"""


if __name__ == "__main__":

    ain='แล้วพบกันใหม่'
    aout=whitespace_string(ain)
    print(ain,'=>',aout)
    aout=whitespace(ain.encode('utf8'))
    print(aout.decode('utf8'))

输出为: 使用python3.2标记化:

python3.2 thai_libthai.py 
แล้วพบกันใหม่ => แล้ว พบ กัน ใหม่
แล้ว พบ กัน ใหม่

对于python3.4,结果字符串为空:

python3.4 thai_libthai.py 
แล้วพบกันใหม่ =>                 

Tags: 程序stringlinelibraryutf8findwhitespaceth

热门问题