Python 常用函数

字符串->数字:

float(str)

int(str)

十六进制字符串转int:

不带0x前缀:

x = int('dead',16)

带0x前缀:

x = int('0xff', 0)

其中第二个参数0必须添加,使得开启int根据字符串猜测类型模式

文件读取:

readline(line) 一行一行读取,line为指定行

readlines 将文件每一行作为元素放入一个列表中:

使用:

for line in f.readlines():

read 读取文件所有内容

查看某一个变量的类型:

type(var)

isInstance(var, classinfo)

classinfo: int, basestring, list, dict, bool

python问题:

使用fromkeys(ind,int(ele))报错TypeError: 'int' object is not iterable

使用sp[ind] = int(ele)就可以了,奇怪的问题

原文地址:https://www.cnblogs.com/lebronzhang/p/6161060.html