进阶的阿牛哥之如何存储每日数据到csv或txt文件(如何实现换行)

一、存储到CSV

import csv


#构造带有表头的csv文件
headers = ('time','yhbdl')

day= [{'time':'20210603','yhbdl':'20.3'},
		{'time':'20210604','yhbdl':'21.1'}]
with open('sz50_yhbdl.csv','a',encoding='utf-8',newline='') as file_obj:
    Dwriter = csv.DictWriter(file_obj,headers)
    Dwriter.writeheader()
    Dwriter.writerows(day)


today= ('20210605','19.9')	#每天获取的数据

#追加到csv文件中:
with open('sz50_yhbdl.csv','a',encoding='utf-8',newline='') as file_obj:
    Dwriter = csv.writer(file_obj)
    Dwriter.writerow(today)

结果:
在这里插入图片描述
二、存储到txt(如何实现换行)

yh_text= '20210605'+'21.11'	#每天获取的数据,以字符串的形式构造


with open('yhbdl.txt','a',encoding='utf-8') as f:
    text = yh_text+'\n'
    f.write(text)

结果:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值