python遍历数组的两种方法

第一种,最常用的,通过for in遍历数组

colours = ["red","green","blue"]
for colour in colours:
    print colour
 
# red
# green
# blue

下面的方法可以先获得数组的长度,然后根据索引号遍历数组,同时输出索引号

colours = ["red","green","blue"]
for i in range(0, len(colours)):
    print i, colour[i]
 
# 0 red
# 1 green
# 2 blue

将print的内容写入文件中

$ python test.py > test.csv

注意: 写入文件类型可以更改。如:test.xls

JSON文件读取和URL读取方法

import json
#json文件读取
f =open('test.json',encoding='utf-8') 
s = json.load(f)

#json读取
f={"name":"ttt","lover":"sing"}
s=json.loads(f)