实例1: v1=u '好神奇的问题!?' type(v1)->unicode v1.decode("utf-8")# not work,because v1 is unicode already v1.encode("gb2312")#work,convert from unicode into gbk2312 [发现] decode是把指定的对象转化为unicode(unicode包含utf-8,utf-16),并且指明了待转化对象的编码方式. encode…
Python之str()与repr()的区别 str()一般是将数值转成字符串,主要面向用户. repr()是将一个对象转成字符串显示,注意只是显示用,有些对象转成字符串没有直接的意思.如list,dict使用str()是无效的,但使用repr可以,这是为了看它们都有哪些值,为了显示之用,主要面向python. 官方文档: The str() function is meant to return representations of values which are fairlyhuman-…
字符串常用操作 移除空白 分割 长度 索引 切片 class str(basestring): """ str(object='') -> string Return a nice string representation of the object. If the argument is a string, the return value is the same object. """ def capitalize(self): &q…
#使用str.format()函数 #使用'{}'占位符 print('I\'m {},{}'.format('Hongten','Welcome to my space!')) >>> I'm Hongten,Welcome to my space! print('#' * 40) #也可以使用'{0}','{1}'形式的占位符 print('{0},I\'m {1},my E-mail is {2}'.format('Hello','Hongten','hongtenzone@fox…
Python中定义常量 都用大写 Pip 安装python第三方模块的命令 一般默认都放在/python27/lib/site-pak List.count(‘元素’)-------------统计list中有几个相同的元素 List.index(‘元素’)---------返回当前(第一个出现的)元素的下标 通过下标来修改list中的相同的值 for i in range(lis.count(90)): nn = lis.index(90) lis[nn] = 999999999…
#capitalize():字符串首字符大写 string = 'this is a string.'new_str = string.capitalize()print(new_str)#输出:This is a string. #center(width, fillchar=None):将字符串放在中间,在指定长度下,首尾以指定字符填充string = 'this is a string.'new_str = string.center(30,'*')print(new_str)#输出:**…
http://stackoverflow.com/questions/24069197/httpresponse-object-json-object-must-be-str-not-bytes HTTPResponse object — JSON object must be str, not 'bytes' up vote17down votefavorite 7 I've been trying to update a small Python library called libpyne…