Python列表写入文件代码


fr = open('aaa.py','r')
lifr = fr.readline()
while lifr!='':
    lifr = lifr.rstrip('\n')  #截掉lifr右边的换行符\n
    print lifr
    lifr=fr.readline()

li = [1,2,3,4,5]
ls = open('aaa.py','a')
ls.write('[1,2,3,4,5]')
ls.close()
ls = open('aaa.py','r')
lr = ls.read() 
print lr
ls.close()
<pre name="code" class="python">li = [1,2,3]
i=0
ls = open('aaa.py','a')
while  i<len(li):
    ls.write('\n'+str(li[i])+'\n')   #将列表数据写入文档,注意write只能写字符串,写入数字时,要转换格式
    i=i+1
ls.close()
ls = open('aaa.py','r')
lr = ls.read()   
print lr
ls.close()


 

你可能感兴趣的:(Python列表写入文件代码)