1、 Read txt file
# Read stu_info.txt The contents of the document , And display the names of all students in the file f = open('stu_info.txt', 'r') lines = f.readlines() print(lines) for line in lines: print(line.split(',')[0]) f.close()
2、CSV File read and write
''' Reading and writing csv file ,csv Comma separated values (Comma-Separated Values,CSV), Sometimes referred to as character separated values , Its files store tabular data in plain text ( Numbers and text ) ''' import csv fr=open('stu_info.csv', 'r') csv_file = csv.reader(fr) for stu in csv_file: print(stu) fr.close() # Yes stu_info.csv Two student information are added to the file Marry and Rom stu=['Marry',28,'Changsha'] stu1=['Rom',23,'Chengdu'] out=open('stu_info.csv','a',newline='') csv_write=csv.write(out,dialect='excel') csv_write.writerow(stu) csv_write.writerow(stu1) out.close()
3、 Read xml File element node
<?xml version="1.0" encoding="UTF-8"?> <Class> <student> <name>Jack</name> <age>28</age> <city>Beijing</city> </student> <student> <name>Bob</name> <age>22</age> <city>Shanghai</city> </student> <student> <name>Marry</name> <age>89</age> <city>Shenzhen</city> </student> <student> <name>Marry</name> <age>89</age> <city>Shenzhen</city> </student> <teacher> <name>Nancy</name> <age>90</age> <city>Shenzhen</city> </teacher> <account> <login username="student" password="123456"/> <login username="teacher" password="88888888"/> </account> </Class>
# see class_info.xml In the document Class Node pair attribute ( The name of the node 、 Node values , Node type ) from xml.dom import minidom # load xml file dom=minidom.parse('class_info.xml') # load dom Object elements root=dom.documentElement # Print node information print(root.nodeName) print(root.nodeValue) print(root.nodeType) ''' nodeName The name of the node nodeValue Returns the value of the text node nodeType attribute , Returns the node type of the node specified by a numeric value If the node is an element node , be nodeType Property will return 1 If the node is an attribute node , be nodeType Property will return 2 '''
4、 Read xml Text node
# Print out separately class_info.xml It contains detailed information about students and teachers ( full name 、 Age 、 City ) from xml.dom import minidom # Get the value of the tag pair dom=minidom.parse('class_info.xml') # Get document object elements root=dom.documentElement # Get the label object according to the label name names=root.getElementsByTagName('name') ages=root.getElementsByTagName('age') citys=root.getElementsByTagName('city') # Print and display separately xml The document label is for the inside and for the content for i in range(4): print(names[i].firstChild.data) print(ages[i].firstChild.data) print(citys[i].firstChild.data) print("----------------")
5、 Read xml The value of the file attribute node
# Read the value of the attribute node : Read and print the account and password of teachers and students respectively from xml.dom import minidom dom = minidom.parse('class_info.xml') root = dom.documentElement logins = dom.documentElement logins = root.getElementsByTagName('login') # obtain login label username attribute for i in range(2): username = logins[i].getAttribute('username') print(username) password = logins[i].getAttribute('password') print(password)
6、 Read the child node information
''' Read the child node information nodeName The name of the node nodeValue Node values nodeType Node type ''' from xml.dom import minidom dom=minidom.parse('class_info.xml') root=dom.documentElement tags=root.getElementsByTagName('student') print(tags[0].nodeName) print(tags[0].tagName) print(tags[0].nodeType) print(tags[0].nodeValue)
7、 Multithreading
# Multithreading from time import ctime, sleep import threading def talk(content, loop): for i in range(loop): print('Start Talk %s %s' % (content, ctime())) sleep(3) def write(content, loop): for i in range(loop): print('Start Write %s %s' % (content, ctime())) sleep(2) # Define and load read / write threads threads = [] t1 = threading.Thread(target=talk, args=('Speak: Hello world', 5)) threads.append(t1) # Execute multithreading t2 = threading.Thread(target=write, args=('Write: gogogo', 5)) threads.append(t2) if __name__ == '__main__': for t in threads: t.start() for t in threads: t.join() print('All the End %r' % ctime())
8、 Multi process
# Multi process from time import ctime, sleep import multiprocessing def talk(content, loop): for i in range(loop): print('Start Talk %s %s' % (content, ctime())) sleep(3) def write(content, loop): for i in range(loop): print('Start Write %s %s' % (content, ctime())) sleep(2) # Define and load read / write processes processs = [] p1 = multiprocessing.Process(target=talk, args=('Speak: Hello world', 5)) processs.append(p1) # Execute multithreading p2 = multiprocessing.Process(target=write, args=('Write: gogogo', 5)) processs.append(p2) if __name__ == '__main__': for p in processs: p.start() for p in processs: p.join() print('All the End %r' % ctime())