1
.取出Unicode中的值:
[u'10115', u'10116', u'10117', u'10118'] 请问下,我想将列表里面的unicode去掉,怎么去 啊?
①
list1 = [u'10115', u'10116', u'10117', u'10118']
list2 = []
for i in list1:
list2.append(i.encode())
list2 = []
for i in list1:
list2.append(i.encode())
②
hostips=[str(hostips) for hostips in A]
---------
2.
时间戳
timearray = time.localtime(charge['data']['object']['created'])
d = time.strftime("%Y-%m-%d %H:%M:%S", timearray)
dt = datetime.datetime.strptime(d, '%Y-%m-%d %H:%M:%S')
*****
1、Python下日期到时间戳的转换
import datetime
import time
dateC=datetime.datetime(2010,6,6,8,14,59)
timestamp=time.mktime(dateC.timetuple())
print timestamp
2、Python下将时间戳转换到日期
import datetime
import time
ltime=time.localtime(1395025933)
timeStr=time.strftime("%Y-%m-%d %H:%M:%S", ltime)
print timeStr
----------
3.
str()和repr()
函数str() 用于将值转化为适于人阅读的形式,而repr() 转化为供解释器读取的形式
4.
- 格式化(format())输出字符串
age = 25
name = 'Caroline'
print('{0} is {1} years old. '.format(name, age)) #输出参数
print('{0:.3} is a decimal. '.format(1/3)) #小数点后三位
print('{0:_^11} is a 11 length. '.format(name)) #使用_补齐空位
print('{first} is as {second}. '.format(first=name, second='Wendy')) #别名替换
print('My name is {0.name}'.format(open('out.txt', 'w'))) #调用方法
print('My name is {0:8}.'.format('Fred')) #指定宽度
|||||||||
Caroline is 25 years old.
0.333 is a decimal.
_Caroline__ is a 11 length.
Caroline is as Wendy.
My name is out.txt
My name is Fred
------------
5.
修改元组中数据
元组中的数据是不可修改的,我可以用
赋值到另一个定义的数据中
str1 =('111')
str2=str1[0].replace('111','222')
当然type(str2) 类型为 str
------------
6.
eval()-->将字符串str当成有效的表达式来求值并返回计算结果
>>>a = "[[1,2], [3,4], [5,6], [7,8], [9,0]]"
>>> b=eval(a)
>>> b
[[1, 2], [3, 4], [5, 6], [7, 8], [9, 0]]
-------------
7.
列表转换为字符串,用','隔开
user_group_android = ['name','pwd','int','cao']
user_android = ','.join(user_group_android) -----则user_android='name,pwd,int,cao'
--------------
8.
时间转换字符串 or 字符串转换成时间
时间->字符串:strftime(按照年月日,时分秒转换成字符串)
xxx=2013-9-8 00:00:00 #正经的时间
startdate = xxx.
strftime('%Y-%m-%d %H:%M:%S') #变为字符串"
2013-9-8 00:00:00"
字符串->时间:strptime
xxx='2013-9-8 00:00:00'
start=time.
strptime(xxx,'
%Y-%m-%d %H:%M:%S') #字符串转化为时间(year=2013,month=9,......)
start_time = datetime.datetime(*start[:6]) #时间修改格式为2009-1-1 00:00:00
-----------------
9.
%i
print '%02d'%1-->01
print '%02d'%12
-->12
print '%02d'%1321-->1321
------------------
10.json.loads() or json.dumps()
json.loads() -----json->dict
json.dumps() -----dict->json
------------------
11.raise 引发异常
raise 引发自己定义的异常,
-----------------
12.
计算时间差
需要使用datetime模块,datetime模块比time模块更能更强。
d1 = datetime.datetime.now()
time.sleep(3)
d2 = datetime.datetime.now()
d = d2-d1
d1 = datetime.datetime.now()
time.sleep(3)
d2 = datetime.datetime.now()
d = d2-d1
d = (d2-d1).days
d.days 天
d.max 最大
d.microseconds 微秒
d.min 最小
d.resolution
d.seconds 秒
d.max 最大
d.microseconds 微秒
d.min 最小
d.resolution
d.seconds 秒
-----------------
13.
字符串转换成列表
a = '1,2,3,4,5,6,7,8'
b = a.split(',')
b
['1', '2', '3', '4', '5', '6', '7', '8']
------------------
14.
修改列表中所有参数的值
a = ['1', '2', '3']
for m , n in enumerate(a):
a[m] = "a little J8--" + n
a
['a little J8--1', 'a little J8--2', 'a little J8--3']
--------------------
15.
修改字典中所有Null为空:
company = {'a': None, 'b': '123', 'c': None}
for index in company.keys():
if company[index] == None:
company[index] = ''
company = {'a': '', 'b': '123', 'c': ''}
----------------------