python文件处理

1、读取txt文件

# 读取stu_info.txt文件内容,并将文件中所有学生名称显示出来

f = open('stu_info.txt', 'r')
lines = f.readlines()
print(lines)

for line in lines:
    print(line.split(',')[0])
    
f.close()

 2、CSV文件读写

'''
读写csv文件,csv即为逗号分隔值(Comma-Separated Values,CSV),
有时也称为字符分隔值,其文件以纯文本形式存储表格数据(数字和文本)
'''

import csv

fr=open('stu_info.csv', 'r')
csv_file = csv.reader(fr)

for stu in csv_file:
    print(stu)

fr.close()

#对stu_info.csv文件追加写入两个学生信息Marry和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、读取xml文件元素节点

<?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>

 

#查看class_info.xml文件里Class节点对属性(节点名称、节点值,结点类型)

from xml.dom import minidom

#加载xml文件
dom=minidom.parse('class_info.xml')
#加载dom对象元素
root=dom.documentElement
#打印结点信息
print(root.nodeName)
print(root.nodeValue)
print(root.nodeType)

'''
nodeName节点名称
nodeValue返回文本节点的值
nodeType属性,返回以数字值指定的节点的节点类型
如果节点是元素节点,则nodeType属性将返回1
如果节点是属性节点,则nodeType属性将返回2
'''

4、读取xml文本节点

#分别打印出class_info.xml里面学生和老师的详细信息(姓名、年龄、城市)

from xml.dom import minidom

#获取标签对的值
dom=minidom.parse('class_info.xml')
#获取文档对象元素
root=dom.documentElement
#根据标签名称获取标签对象
names=root.getElementsByTagName('name')
ages=root.getElementsByTagName('age')
citys=root.getElementsByTagName('city')

#分别打印显示xml文档标签对里面对内容
for i in range(4):
    print(names[i].firstChild.data)
    print(ages[i].firstChild.data)
    print(citys[i].firstChild.data)
    print("----------------")

 5、读取xml文件属性节点的值

# 读取属性节点的值:分别读取打印老师和学生的账号密码

from xml.dom import minidom

dom = minidom.parse('class_info.xml')
root = dom.documentElement

logins = dom.documentElement

logins = root.getElementsByTagName('login')

# 获取login标签username属性
for i in range(2):
    username = logins[i].getAttribute('username')
    print(username)
    password = logins[i].getAttribute('password')
    print(password)

 6、读取子节点信息

'''
读取子节点信息
nodeName 节点名称
nodeValue 节点值
nodeType 节点类型
'''

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、多线程

# 多线程

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)


# 定义和加载读写线程
threads = []
t1 = threading.Thread(target=talk, args=('Speak: Hello world', 5))
threads.append(t1)

# 执行多线程
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、多进程

# 多进程

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)


# 定义和加载读写进程
processs = []
p1 = multiprocessing.Process(target=talk, args=('Speak: Hello world', 5))
processs.append(p1)

# 执行多线程
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())

 

posted on 2019-02-22 15:38  lina2014  阅读(133)  评论(0编辑  收藏  举报

导航