hallo编程
陈钧
你若盛开,清风自来。
展开
-
【Python】写文件的不同方式
output = open('data.txt', 'w') #写文本文件output = open('data.txt','wb') #写二进制文件output = open('data.txt','a') #追加写文件output.write("\n写进内容。")output.close() #写完后一定要关闭文件原创 2014-03-11 08:36:02 · 823 阅读 · 0 评论 -
【Python】如何判断一个字符串为空
#方法一if oneString: print "not empty"else: print "empty"#方法二if oneString == "": print "empty"else: print "not empty"原创 2014-03-13 21:18:46 · 127852 阅读 · 0 评论 -
【Python】KeyError解决方法
#如果不知道dict中是否有key的值dict.get(key)#如果用 dict[key] 读取会报KeyError异常原创 2014-03-13 21:13:43 · 77383 阅读 · 0 评论 -
【Python】保留有效位
a=b/float(c) #例如此时 a=4.323232324343#输出到文件中outputFile.write(str('%.3f'%a)+'\n') #保留小数点三位有效数字#直接输出print 'a=%0.3f, b=%d'%(a,b)原创 2014-03-11 22:18:26 · 9858 阅读 · 0 评论 -
【Python】多次访问文件,指针再次指向第一行
inputFile = open('xiao.csv','r')line = inputFile.readline() #先初始化,指向文件头while line: name = line.split(',')[6] line = inputFile.readline() #遍历读下一行#while循环结束后,指针到文件末尾inputFile.seek(0) #返回文件头line原创 2014-03-11 08:45:16 · 9360 阅读 · 0 评论 -
【Python】判断一个变量的数据类型的两种方法
法1: import typestype(x) is types.IntType # 判断是否int 类型type(x) is types.StringType #是否string类型法2: import typestype(x) == type(1) # 是否 int 类型type(x) in (type('u') , type(''))# 是否string, unic转载 2014-03-11 08:31:58 · 1833 阅读 · 0 评论 -
【Python】if.. elif..
在python里if .. else if是这样子的:if .. elifif a>1: a=1elif a a=2原创 2014-03-10 21:57:28 · 562 阅读 · 0 评论 -
【Python】Error:unident does not match any outer indentation level
#缩进问题,可以用PyCharm编辑器原创 2014-03-11 15:40:46 · 3500 阅读 · 0 评论 -
【Python】对于不确定的内容,try .. except ..
try: time= line.split(',')[2] timeStamp= float(time)#有时time为空,或者为字符串形式,就会出现cannot convert string to float这样的错误,所以在此放进try里....except: time = 0# 也可以用pass ,什么操作都不做原创 2014-03-11 08:39:14 · 850 阅读 · 0 评论 -
【Python】并列&&-》and
if a>b and c>b: print 123原创 2014-03-11 08:28:53 · 4866 阅读 · 0 评论 -
【Python】Error:'int' object is not callable
if len(a) == len(b):#最终出现'int' object is not callable的错误#说明在前面定义了一个len 同名的变量 如 len = len("hallo Jun")原创 2014-03-11 10:49:44 · 15397 阅读 · 0 评论