Python的文件操作还是很方便的。
f=open("test.txt","r") #以读取模式r,打开文件 for line in f: print(line) #返回字符串"123,abc "和"888,ppp ",注意在尾部的换行符 line="abc,123 " line1=line.strip() #line1="abbc,123" f.close() #或使用with,此时可以不用写f.close(),离开with块后系统会自动关闭文件 with open("test.txt","r") as f: for line in f: print(line) f1=open("test2.txt","w") #以写模式w,打开文件,如果已有该文件,自动清空其内容 s="this is a good day " f1.write(s) s="abc,123 " f1.write(s) f1.close() #在test2.txt中写入两行this is a good day和abc,123,注意行位的换行符 with open("test3.txt","a") as f: #以追加方式打开,如果文件有内容,在尾部添加新内容 s="this is a good day " f.write(s) x=2.545 x=round(x) x=5 y=bin(5)[2:] x=1 dic={'a':1,'b':2} ll=dic.keys() mm=list(ll) for i in dic.keys(): x=dic[i] #dic['c']=3 #dic['c']=dic.get('c',0)+1 x=2//3 x=4 x=[1,2,3] y=x[1:1] x=1 x='123 abs' x1=x.center(2)