'''导入txt文件'''
f = open('./abc.txt','r')
lines = f.readlines()
print(lines)
'''导入csv文件'''
import csv
csv_file = csv.reader(open('./abc.csv','r'))
for stu in csv_file:
print(stu)
'''写入csv文件'''
import csv
so = ['姓名','年龄','日期']
stu = ['marry',34,'2020/9/10']
sto = ['bob',50,'2020/9/10']
out = open('stu.csv','a',newline='')
csv_write = csv.writer(out,dialect='excel')
csv_write.writerow(so)
csv_write.writerow(stu)
csv_write.writerow(sto)
print('over')
'''导入xml'''
from xml.dom import minidom
dom = minidom.parse('./abc.xml')
root = dom.documentElement
ids = root.getElementsByTagName("id")
names = root.getElementsByTagName("name")
ages = root.getElementsByTagName("ages")
citys = root.getElementsByTagName("city")
logins = root.getElementsByTagName("login")
# for i in range(3):
# print(ids[i].firstChild.data)
# print(names[i].firstChild.data)
# print(citys[i].firstChild.data)
username = logins[0].getAttribute('username')
password = logins[0].getAttribute('password')
print(username,password)
'''单线程'''
from time import ctime,sleep
def talk():
print('1 %s'%ctime())
sleep(2)
def write():
print('2 %s'%ctime())
sleep(3)
if __name__ == '__main__':
talk()
write()
print('3 %s' % ctime())
'''多线程'''
from time import ctime,sleep
import threading
def talk(content,loop):
for i in range(loop):
print('1 %s %s'%(content,ctime()))
sleep(3)
def write(content,loop):
for i in range(loop):
print('2 %s %s' % (content,ctime()))
sleep(5)
threads = []
t1 = threading.Thread(target=talk,args=('hello!',2))
threads.append(t1)
t2 = threading.Thread(target=write,args=('人生苦短!',6))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
for t in threads:
t.join()
print('结束了 %s'%ctime())
'''多进程'''
from time import ctime,sleep
import multiprocessing
def talk(content,loop):
for i in range(loop):
print('1 %s %s'%(content,ctime()))
sleep(3)
def write(content,loop):
for i in range(loop):
print('2 %s %s' % (content,ctime()))
sleep(5)
threads = []
t1 = multiprocessing.Process(target=talk,args=('hello!',2))
threads.append(t1)
t2 = multiprocessing.Process(target=write,args=('人生苦短!',6))
threads.append(t2)
if __name__ == '__main__':
for t in threads:
t.start()
for t in threads:
t.join()
print('结束了 %s'%ctime())
Python各种文件导入导出
最新推荐文章于 2024-06-27 17:55:30 发布