首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python ctypes程序可以在3.2上运行,但在3.4上不兼容

Python ctypes程序可以在3.2上运行,但在3.4上不兼容
EN

Stack Overflow用户
提问于 2015-08-19 23:41:09
回答 1查看 125关注 0票数 0

我有一个程序调用libtahi C库来标记泰语文本。这个程序在python3.2上运行正常,但在python3.4上运行失败。你知道3.4版本失败的原因吗?

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

代码语言:javascript
复制
#!/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进行标记化:

代码语言:javascript
复制
python3.2 thai_libthai.py 
แล้วพบกันใหม่ => แล้ว พบ กัน ใหม่
แล้ว พบ กัน ใหม่

在python3.4中,结果字符串为空:

代码语言:javascript
复制
python3.4 thai_libthai.py 
แล้วพบกันใหม่ =>                 
EN

回答 1

Stack Overflow用户

发布于 2015-08-20 13:49:06

使用aout = (c_wchar * (2 * len(ain) + 1))()使代码可以在Python3.2和3.4上运行。感谢eryksun!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32100067

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档