python基礎-讀取文件
阿新 • • 發佈:2017-07-24
() int 拆分 span open div lose color 本地
一、讀取txt文件
#coding=utf-8 user_file = open(‘./bosspe/data/users.txt‘,‘r‘)#以只讀方式打開txt文件 lines = user_file.readlines()#按行讀取文件中的內容 user_file.close()#關閉文件 #循環遍歷每一行數據的用戶名和密碼,並打印 for line in lines: username = line.split(‘,‘)[0]#使用split()方法拆分出用戶和密碼 password = line.split(‘,‘)[1] print(username,password)
二、讀取csv文件
#coding=utf-8 import csv #導入csv包 #讀取本地CSV文件,文件是xls文件另存為csv格式創建 user_file = open(‘./bosspe/data/users.csv‘,‘r‘) data = csv.reader(user_file) #循環輸出每一行信息 for user in data: print(user)
三、讀取xml文件
python基礎-讀取文件