WebDriver read data files --- (Mushishi "selenium3 automated testing combat - based on the Python language note 28")

1. Read txt file

read() Reads the entire file
readline() Reads one line of data
readlines() Read all lines of data

 

 

 

 

# Read the file 
with (Open ( " ./webframe/test.txt " , " R & lt " )) AS user_file: 
    Data = user_file.readlines () 

# formatting 
Users = []
 for Line in Data: 
    User = Line [ : -1] .split ( " : " ) 
    users.append (the User) 

# print a two-dimensional array of users 
Print (users)

2. Read CSV file

Import CSV
 Import the codecs   # module encoder and decoder 
from the itertools Import islice 

# reading local CSV file 
Data = csv.reader (codecs.open ( " ./webframe/test.csv " , ' R & lt ' , ' UTF-SIG. 8- ' )) 

# store user data 
users = [] 

# cycles each line of output information 
for line in islice (data,. 1, None):   # islice (): the first parameter specified object, the second parameter specifies the location of the iteration The third parameter specifies the end position 
    users.append (Line) 

# print 
Print (the Users)

3. Read xml file

config.xml:

<?xml version="1.0" encoding="utf-8" ?>
<info>
    <plaforms>
    <plaforms>Windows</plaforms>
    <plaforms>macOS</plaforms>
    <plaforms>Linux</plaforms>
    </plaforms>

    <browsers>
            <browsers>Firefox</browsers>
            <browsers>Chrome</browsers>
            <browsers>Edge</browsers>
    </browsers>

    <url>http:www.xxx.com</url>
    <login username="admin" password="123456"/>
    <login username="guest" password="654321"/>

(1) Acquisition of data between the tags

from xml.dom.minidom Import the parse 

# Open xml 
dom = the parse ( " ./webframe/config.xml " ) 

# get the document element object 
root = dom.documentElement 

# get (set) label 
tag_name = root.getElementsByTagName ( " os " ) 

Print (the tag_name [0] .firstChild.data)
 Print (the tag_name [. 1 ] .firstChild.data)
 Print (the tag_name [2] .firstChild.data)

(2) the get property value tag

from xml.dom.minidom Import the parse 

# open XML 
DOM = the parse ( " ./webframe/config.xml " ) 

# Get the document element object is 
the root = dom.documentElement 

# obtain (a group of) tags 
login_info root.getElementsByTagName = ( ' Login ' ) 

# Get username attribute value of the second tag login 
username = login_info [1] .getAttribute ( ' username ' )
 Print (username) 

# Get the value of the second password attribute tag login 
username = login_info [1] .getAttribute ( ' password ' )
 Print(password)

4. Read json file

import json
with open("./webframe/test.json", "r") as f:
    data = f.read()
    
user_list = json.loads(data)
print(user_list)

 

Guess you like

Origin www.cnblogs.com/kite123/p/11536046.html