python 记录一下

1.字符串数字,逗号分隔,转换成list,list中string类型转换成int类型

   data=“12,23,34,56”

  #逗号分隔符
  valueList = data.split(',')
  print(valueList)
  valueList=["12","23","34","56"]
  #list string类型转换为int
  valuedata=list(map(eval,valueList))

  valuedata=[12,23,34,56]

2.浮点数小数和整数分离,且小数保留前5位数
  i=112.23456
  #小数整数分离
  zs, xs = str('{:.5f}'.format(i)).split('.')

3.字符串十进制数据转换成16进制数据,且只获取前2位或前六位
  intpart = "{:0>2}".format(hex(int(zs))[2:])
  flopart="{:0>6}".format(hex(int(xs))[2:])
4.如果valuedata是一个列表,for循环中i获取的是列表中的值for i in valuedata:
  获取i在列表的中索引位置 valuedata.index(i)

5.for循环中循环12次,则i对应的是循环第几次,for i in range(12):

6.import一个类,报错:TypeError: 'module' object is not callable ,调用的脚本,类写的有问题

 from Simulat_instruction_report.Ridingdata.Cal_distance.Common import Log

log = Log.Log()

7.运行报错:TypeError: must be str, not NoneType
数据类型不是str,转换成str

8.运行报错:ValueError: too many values to unpack (expected 3)

9.时间格式转换为时间戳
timeArray = time.strptime(a, "%Y/%d/%m %H:%M:%S")
timeStamp = "{:1}".format(int(time.mktime(timeArray)))

时间格式转换为13位的时间戳
a = "2019/26/08 16:16:33"
time_stamp = time.strptime(a, "%Y/%d/%m %H:%M:%S")
timeArray = datetime.datetime.strptime(a, "%Y/%d/%m %H:%M:%S")
timeStamp = str(int(time.mktime(time_stamp)))
data_microsecond = str("%06d" % timeArray.microsecond)[0:3]
date_stamp = timeStamp + data_microsecond

时间戳转为时间
timeArray = time.localtime(uptimestamp)
self.uploadtime = time.strftime("%Y/%m/%d %H:%M:%S", timeArray)
 10.获取字典里面的某个值:
def zddq(dict,cmdvalue):
for k,v in dict.items():
if k == cmdvalue:
getcode=v
break
elif v ==cmdvalue:
getcode=k
break
else:
getcode="没有值"
return getcode

10.为多个变量数值:a,b,c=2,15,13
11.变量值互换:a,b=b,a
12.链式比较:0<=b<=a<100
13.any/all :any是任何一个元素为真就返回true,否则返回False;all是所有元素为真返回True,否者返回False
14.打开文件:
  with open('result.txt','w') as fp:
    fp.write('just for fun!')
 
 


 

 
 
posted @ 2019-08-14 17:04  大人的春天  阅读(238)  评论(0编辑  收藏  举报