1. 需要输出3列,为了输出好看,需要制定每一列的宽度:
‘%6.2f’ % 1.235
# 长度为6,保留2为小数
print '{0:20} {1:<20} {1:<20}\r\n'.format(ip.strip(),jsondata['isv'],jsondata['isp'])#20表示长度为20,不足的用空格补充,<代表左对齐
58.208.184.96 0 0
27.184.27.79 0 0
114.218.150.66 0 0
119.54.61.221 0 0
113.250.159.124 0 0
50.200.116.246 0 0
222.76.172.140 0 0
< (默认)左对齐
> 右对齐
^ 中间对齐
= (只用于数字)在小数点后进行补齐
2. 表
格式 | 描述 |
---|---|
%% | 百分号标记 |
%c | 字符及其ASCII码 |
%s | 字符串 |
%d | 有符号整数(十进制) |
%u | 无符号整数(十进制) |
%o | 无符号整数(八进制) |
%x | 无符号整数(十六进制) |
%X | 无符号整数(十六进制大写字符) |
%e | 浮点数字(科学计数法) |
%E | 浮点数字(科学计数法,用E代替e) |
%f | 浮点数字(用小数点符号) |
%g | 浮点数字(根据值的大小采用%e或%f) |
%G | 浮点数字(类似于%g) |
%p | 指针(用十六进制打印值的内存地址) |
%n | 存储输出字符的数量放进参数列表的下一个变量中 |
import datetime
d = datetime.datetime.now()
def day_get(d):
oneday = datetime.timedelta(days=1)
day = d - oneday
date_from = datetime.datetime(day.year, day.month, day.day, 0, 0, 0)
date_to = datetime.datetime(day.year, day.month, day.day, 23, 59, 59)
print '---'.join([str(date_from), str(date_to)])
def week_get(d):
dayscount = datetime.timedelta(days=d.isoweekday())
dayto = d - dayscount
sixdays = datetime.timedelta(days=6)
dayfrom = dayto - sixdays
date_from = datetime.datetime(dayfrom.year, dayfrom.month, dayfrom.day, 0, 0, 0)
date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
print '---'.join([str(date_from), str(date_to)])
def month_get(d):
dayscount = datetime.timedelta(days=d.day)
dayto = d - dayscount
date_from = datetime.datetime(dayto.year, dayto.month, 1, 0, 0, 0)
date_to = datetime.datetime(dayto.year, dayto.month, dayto.day, 23, 59, 59)
print '---'.join([str(date_from), str(date_to)])