python2 python3 字符串 编码格式 处理
使用python的ctypes调用c语言中的函数,传入字符串,打印输出异常。解决方法记录于此。
参考连接:
http://blog.csdn.net/u011546806/article/details/44936303
主要原因是编码格式不同导致的。python2和python3采用的默认编码不同。
python2默认就是str和unicode,str和unicode可以直接进行连接。python3默认的字符串编码是bytes和str。如果要操作unicode格式的,需要通过encode()函数先转换。
c文件如下。
test.c
#include <stdio.h>
int test(char *temp)
{
printf("temp:%s\n", temp);
return 0;
}
test.py
#!/usr/bin/env python
import test
import os
from ctypes import *
test = cdll.LoadLibrary(os.getcwd() + '/libtest.so')
test.test('hello')
python2运行脚本输出如下
qt@tony:/mnt/hgfs/Desktop/$ /usr/bin/python2 test.py
temp:hello
temp:hello
python3运行结果
qt@tony:/mnt/hgfs/Desktop/$ /usr/bin/python3 test.py
temp:h
temp:h
解决方法,使用encode('utf-8')将字符串转化成unicode格式.
更改如下
#!/usr/bin/env python
import test
import os
from ctypes import *
test = cdll.LoadLibrary(os.getcwd() + '/libtest.so')
# 将str转换成unicode
test.test('hello'.encode('utf-8'))
Tony Liu
2017-6-2, Shenzhen
python2 python3 字符串 编码格式 处理的更多相关文章
- Python3字符串 详解
Python3 字符串 字符串是 Python 中最常用的数据类型.我们可以使用引号('或")来创建字符串. 创建字符串很简单,只要为变量分配一个值即可. Python 访问字符串中的值 P ...
- 一篇文章助你理解Python2中字符串编码问题
前几天给大家介绍了unicode编码和utf-8编码的理论知识,没来得及上车的小伙伴们可以戳这篇文章:浅谈unicode编码和utf-8编码的关系.下面在Python2环境中进行代码演示,分别Wind ...
- python3字符串
Python3 字符串 Python字符串运算符 + 字符串连接 a + b 输出结果: HelloPython * 重复输出字符串 a*2 输出结果:HelloHello [] 通过索引获取字符串中 ...
- python2 python3区别
Python开发团队将在2020年1月1日停止对Python2.7的技术支持,但python2的库仍然比较强大(在 pip 官方下载源 pypi 搜索 Python2.7 和 Python3.5 的第 ...
- python2&python3
1.Python3 使用 print 必须要以小括号包裹打印内容,比如 print('hi') Python2 既可以使用带小括号的方式,也可以使用一个空格来分隔打印内容,比如 print 'hi ...
- python3 字符串属性(一)
python3 字符串属性 >>> a='hello world' >>> dir(a) ['__add__', '__class__', '__contains_ ...
- 从python2,python3编码问题引伸出的通用编码原理解释
今天使用python2编码时遇到这样一条异常UnicodeDecodeError: ‘ascii’ code can’t decode byte 0xef 发现是编码问题,但是平常在python3中几 ...
- (十四)Python3 字符串格式化
Python3 字符串格式化 字符串的格式化方法分为两种,分别为占位符(%)和format方式.占位符方式在Python2.x中用的比较广泛,随着Python3.x的使用越来越广,format方式使用 ...
- python2 python3区别(续)
1.除法 Python2 Python3 int/int → int int/int → float python2下整数除以整数返回整数类型,python3下整数除以整数返回浮点数类型 当某些语句假 ...
随机推荐
- NLP资料
https://towardsdatascience.com/how-to-get-started-in-nlp-6a62aa4eaeff?source=rss-299377188126------3
- Linux下用netstat查看网络状态、端口状态
在linux一般使用netstat 来查看系统端口使用情况步. netstat命令是一个监控TCP/IP网络的非常有用的工具,它可以显示路由表.实际的网络连接以及每一个网络接口设备的 netstat命 ...
- 服务器搭建5 Samba实现文件共享
Samba服务器工作原理 客户端向Samba服务器发起请求,请求访问共享目录,Samba服务器接收请求,查询smb.conf文件,查看共享目录是否存在,以及来访者的访问权限,如果来访者具有相应的权限, ...
- jqgrid传入的json数据,赋值了但是没有在表格上显示
1. datatype: "local",属性加入这个 2. dataValue就是json对象 for (var i = 0; i <= dataValue.length ...
- (转)Using Python3.5 in Ubuntu - Trusty
转自:https://www.reddit.com/r/IPython/comments/3lf81w/using_python35_in_ubuntu_trusty/Note:照这个方案安装pyth ...
- 【web技术】html特效代码(二)
html特效代码(一) html特效代码(二) 图片漂浮广告代码 <bodybgcolor="#F7F7F7"> <!--图片漂浮广告代码开始--> < ...
- LeetCode: Merge k Sorted Lists 解题报告
Merge k Sorted Lists Merge k sorted linked lists and return it as one sorted list. Analyze and descr ...
- eclipse中git更新操作
1,本地已经修改的代码不要commit和push 2,选中工程,右击Team,然后点击Fetch from Upstream,从远程服务器拉取最新的代码 3,上一步操作完成,再右击工程,选中Team, ...
- JAVA-JSP内置对象之request获得封装所有参数值的Map
相关资料:<21天学通Java Web开发> 获得封装所有参数值的Map1.通过request对象的getParameterMap()方法来获得封装所有的参数值的Map对象.2.通过该Ma ...
- C#学习笔记(2)——操作sqlite数据库增删改查
说明(2017-5-25 10:49:50): 1. app.config文件 Alt+Shift+C创建类,选择“应用程序配置文件”,添加<connectionStrings>,要先打个 ...