Python的常用小技巧

1、读取CSV文件的某一列,column的数据类型是List。

#codint=utf-8
import csv
path='/Users/cindy/Desktop/changjingci.csv' with open(path,'rb') as csvfile: reader = csv.reader(csvfile) column = [row[0] for row in reader]

 

2、可以查下数据的类型,在调试的过程中需要查询某个数据类型。type(变量)

 

3、当前的str数据类型如果有空格的话,需要去空格的方法:

strip():把头和尾的空格去掉;lstrip():把左边的空格去掉;rstrip():把右边的空格去掉

 

4、String转成Json串,然后可以直接获取字段的内容了。

data = json.loads(content)
s=data['data']['shopList']

 

5、List的长度是len(strlist)

 

6、String的字段是空的判断:

s=' '
if s.strip()=='':
    print 's is null'
或者
if not s.strip():
    print 's is null'

 

posted @ 2017-08-02 18:40  励志SQA女  阅读(131)  评论(0编辑  收藏  举报