python最经典的打开文件的操做就是open函数python
fp = open('test') #以只读的方式打开文件名为'test'的文件(该文件在同目录下,不然要绝对路径) content = fp.read() #读取内容存储为字符串形式 fp.close() #关闭文件
该test文件通常为txt文件,读取文件常见的有三种方法dom
read(...) read([size]) -> read at most size bytes, returned as a string. 返回整个文件以字符串形式存储 readline(...) readline([size]) -> next line from the file, as a string. 返回文件中的一行数据以字符串形式存储 readlines(...) readlines([size]) -> list of strings, each a line from the file. 返回文件的全部行,以列表的形式存储全部行
eg:函数
test.txt测试
test read "这是一个测试文件" yes you can read and write for this file
read.pythis
#coding:utf-8 fp = open("test", "r+") #可读可写 lines = fp.readlines() print lines for line in lines: print line fp.close() ------------------------------------------------------------ ['test read\n', '\n', '"\xe8\xbf\x99\xe6\x98\xaf\xe4\xb8\x80\xe4\xb8\xaa\xe6\xb5\x8b\xe8\xaf\x95\xe6\x96\x87\xe4\xbb\xb6"\n', '\n', 'yes you can read and write for this file'] test read "这是一个测试文件" yes you can read and write for this file ------------------------------------------------------------- 从结果来看lines是遇到'\n'就认为是一行数据
读取csv数据,须要用到csv库,必定要是csv文件,而不是xlsx文件url
information.csvspa
在python中实际读取的数据是这样的excel
terry,123456@126.com,23,man samsun,123434@163.com,31,woman alisa,mm@qq.com,21,woman lafen,lafen@msn.com,34,man ------------------------------------------- 以逗号隔开元素
经过csv库进行读取,并打印出用户名与邮箱code
read_csv.pyorm
import csv datas = csv.reader(open('infomation.csv', 'Ur'), dialect=csv.excel_tab) print datas #是一个csv对象 for data in datas: print type(data) print data #data是一个列表,里面只有一个字符串元素['terry,123456@126.com,23,man'] print data[0] username = data[0].split(',')[0] #data[0]= 'terry,123456@126.com,23,man' mail = data[0].split(',')[1] print username, mail ------------------------------------------------------------------------ <type '_csv.reader'> <type 'list'> ['terry,123456@126.com,23,man'] terry,123456@126.com,23,man terry 123456@126.com <type 'list'> ['samsun,123434@163.com,31,woman'] samsun,123434@163.com,31,woman samsun 123434@163.com <type 'list'> ['alisa,mm@qq.com,21,woman'] alisa,mm@qq.com,21,woman alisa mm@qq.com <type 'list'> ['lafen,lafen@msn.com,34,man'] lafen,lafen@msn.com,34,man lafen lafen@msn.com
经过python 读取xml的数据
info.xml
<?xml version="1.0" encoding="utf-8"?> <info> <base> <platform>Windows</platform> <browser>FireFox</browser> <url>http://www.baidu.com</url> <login username='admin' password='1234545'/> <login username="guest" password="654321"/> </base> <test> <province>北京</province> <province>广东</province> <city>深圳</city> <city>珠海</city> <province>浙江</province> <city>杭州</city> </test> </info>
经过python导入xml.dom来读取xml上的结点及数据
read_xml.py
from xml.dom import minidom dom = minidom.parse('info.xml') #取出dom root = dom.documentElement #取出根结点 logins = root.getElementsByTagName('login') #经过dom寻找结点login,能够找到两个 username = logins[0].getAttribute("username") #<login username='admin' password='1234545'/>取出username的属性值 print username password = logins[0].getAttribute("password") #<login username='admin' password='1234545'/>取出password的属性值 print password provices = dom.getElementsByTagName("province") #寻找标签为province的节点,有3个 citys = dom.getElementsByTagName("city") #寻找标签为citys的节点,也有3个 p2 = provices[1].firstChild.data #标签对之间的值 print p2 c1 = citys[0].firstChild.data print c1 --------------------------------------------------------- admin 1234545 广东 深圳
参考:
《Selenium2自动化测试实战》