for row in ls:
<输出文件>.write(",".join(row)+'\n')
例:
fo=open('/home/np/下载/price2.cvs','w+')
ls=[['北京''101.5','120.7','121.4'],['上海''1.1','2','3']]
for row in ls:
fo.write(",".join(row)+"\n")
fo.close()
输出结果为:
3.导入CVS格式数据到列表:
3.1)一次性读入全部数据写入列表
fo=open('/home/np/下载/price.cvs')
ls=[];
for line in fo:
line=line.replace('\n',"")
ls.append(line.split(","))
print(ls)
fo.close()
Spyder3输出为:
runfile('/home/np/下载/test.py', wdir='/home/np/下载')
[['北京', '101.5', '120.7', '121.4']]
3.2)逐行处理CVS格式数据:
从CVS文件中读取数据,去掉内容中的逗号,打印到屏幕。
fo=open('/home/np/下载/price2.cvs')
ls=[];
for line in fo:
line=line.replace('\n',"")
ls.append(line.split(","))
lns=""
for s in ls:
lns+="{}\t".format(s)
print(lns)
fo.close()