1. 程式人生 > >python day08作業答案

python day08作業答案

1.
a
f=open('11.txt','r',encoding='utf-8')
a=f.read()
print(a)
f.flush()
f.close()
b.
f=open('11.txt','a',encoding='utf-8')
f.write('信不信由你')
f.flush()
f.close()
c.
f=open('11.txt','r+',encoding='utf-8')
a=f.read()
print(a)
f.write('信不信由你')
f.flush()
f.close()
d.
f=open('11.txt','w',encoding='utf-8')
f.write(
'每天堅持一點') f.flush() f.close() e. f1=open('11.txt','r',encoding='utf-8') f2=open('11.1','w',encoding='utf-8') a=f1.readline() b=f1.readline() c=f1.readline() d=f1.readline() f2.write(a) f2.write(b) f2.write(c) f2.write('你們就信了吧。') f2.write(d) 2. a,b with open('t1.txt','r+',encoding='utf-8') as f1: a
=f1.read() f1.write('a') for line in a: print(line) c. with open('t1.txt','r',encoding='utf-8') as f1: a=f1.readlines() for line in a: print(line) d. with open('t1.txt','r',encoding='utf-8') as f1: a=f1.read(4) print(a) e. with open('t1.txt','r',encoding='utf-8') as f1: a=f1.readline() a.replace(
' ','') a.replace('/n','') print(a) f. with open('t1.txt','r',encoding='utf-8') as f1: a=f1.readline() b=f1.readline() c=f1.readline() print(c) g. with open('t1.txt','a+',encoding='utf-8') as f1: f1.write('老男孩教育') f1.seek(0) a=f1.read() print(a) h. with open('t1.txt','r',encoding='utf-8') as f1: a = f1.read() print(a) 3. b=[] with open('a.txt','r',encoding='utf-8') as f1: df=f1.readlines() for i in range(0,len(df)): a = {} a['name']=df[i].replace('\n','').split(' ')[0] a['price'] = df[i].replace('\n','').split(' ')[1] a['amount'] = df[i].replace('\n','').split(' ')[2] b.append(a) print(b) 4. f1= open('4.txt','r',encoding='utf-8') f2=open('4.1.txt','w+',encoding='utf-8') a=f1.read() c=a.replace('alex','SB') f2.write(c) 5. lst=[] f1= open('a.txt','r',encoding='utf-8') for i in f1: lst1=i.split() print(lst1) dic={} dic[lst1[0].split(':')[0]]=lst1[0].split(':')[1] dic[lst1[1].split(':')[0]]=lst1[1].split(':')[1] dic[lst1[2].split(':')[0]] =lst1[2].split(':')[1] dic[lst1[3].split(':')[0]] = lst1[3].split(':')[1] lst.append(dic) print(lst) 6. lst=[] f1= open('a.txt','r',encoding='utf-8') a=f1.readline().split() print(a) for i in f1: dic={} print(i) dic[a[0]]=i.split()[0] dic[a[1]]=i.split()[1] dic[a[2]]=i.split()[2] lst.append(dic) print(lst)