练习(二)
1 #1. 把一个数字的list从小到大排序,然后写入文件,然后从文件中读取出来文件内容,然后反序,在追加到文件的下一行中 2 3 4 a=[1,234,5,7,8,9,5,343] 5 a.sort() 6 with open('11.txt', 'w')as f1: 7 f1.write(str(a)) 8 print(a) 9 a.reverse() 10 with open('11.txt','a')as f2: 11 f2.write(str(a)) 12 print(a) 13 14 #2. 分别把 string, list, tuple, dict写入到文件中 15 #string 16 a=('1','2','3') 17 with open('12.txt','w')as f1: 18 f1.write(str(a)) 19 20 #list 21 b=[1,2,3,4,5,6,7,8,9] 22 with open('12.txt','a')as f2: 23 f2.write(str(b)) 24 25 #tuple 26 c=('asdasd','sdasdww','wqqweewq') 27 with open('12.txt','a')as f3: 28 f3.write(str(c)) 29 30 #dict 31 d={'qw1':'we23'} 32 with open('12.txt','a')as f4: 33 f4.write(str(d)) 34 with open('12.txt','r')as f5: 35 print(f5.read())