python文件讀取

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自動化測試實戰》

相關文章
相關標籤/搜索