#1.open 读文本文件,open需与close()配对使用
#coding=utf-8# r 可省略,默认为r source = open("D:\pythonTest\data\data01.txt","r")try: values = source.read() print valuesfinally: source.close()
#一行行地读文本source = open("D:\pythonTest\data\data01.txt")lines =source.readlines()#通过遍历每行for line in lines: print linesource.close()
#2. 打开csv文件
csv.reader()
import csvdef loginData(): my_file = "D:\\pythonTest\\data\loginData.csv" datas = csv.reader(file(my_file,'rb')) return datas''' for data in datas: openUrl = data[0] username = data[1] password = data[2] return data'''
#3.打开 csv 方法: with open('xxx.csv','rb') as f:
#读取csv文件,文档有表头with open('D:\pythonTest\data\loginData02.csv','rb') as csvfile: reader = csv.DictReader(csvfile) urls = [] usernames = [] passwords= [] for row in reader: #需知道列名 if row['if_excute'] != 'if_excute': url = row['url'] username = row['username'] password = row['password'] urls.append(url) usernames.append(username) passwords.append(password) print urls print passwords print usernames