[{"createTime":1735734952000,"id":1,"img":"hwy_ms_500_252.jpeg","link":"https://activity.huaweicloud.com/cps.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=V1g3MDY4NTY=&utm_medium=cps&utm_campaign=201905","name":"华为云秒杀","status":9,"txt":"华为云38元秒杀","type":1,"updateTime":1735747411000,"userId":3},{"createTime":1736173885000,"id":2,"img":"txy_480_300.png","link":"https://cloud.tencent.com/act/cps/redirect?redirect=1077&cps_key=edb15096bfff75effaaa8c8bb66138bd&from=console","name":"腾讯云秒杀","status":9,"txt":"腾讯云限量秒杀","type":1,"updateTime":1736173885000,"userId":3},{"createTime":1736177492000,"id":3,"img":"aly_251_140.png","link":"https://www.aliyun.com/minisite/goods?userCode=pwp8kmv3","memo":"","name":"阿里云","status":9,"txt":"阿里云2折起","type":1,"updateTime":1736177492000,"userId":3},{"createTime":1735660800000,"id":4,"img":"vultr_560_300.png","link":"https://www.vultr.com/?ref=9603742-8H","name":"Vultr","status":9,"txt":"Vultr送$100","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":5,"img":"jdy_663_320.jpg","link":"https://3.cn/2ay1-e5t","name":"京东云","status":9,"txt":"京东云特惠专区","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":6,"img":"new_ads.png","link":"https://www.iodraw.com/ads","name":"发布广告","status":9,"txt":"发布广告","type":1,"updateTime":1735660800000,"userId":3},{"createTime":1735660800000,"id":7,"img":"yun_910_50.png","link":"https://activity.huaweicloud.com/discount_area_v5/index.html?fromacct=261f35b6-af54-4511-a2ca-910fa15905d1&utm_source=aXhpYW95YW5nOA===&utm_medium=cps&utm_campaign=201905","name":"底部","status":9,"txt":"高性能云服务器2折起","type":2,"updateTime":1735660800000,"userId":3}]
1.writelines()直接写入
l=["A","B","C","D"] f=open("k.txt","w") f.writelines(l) f.close()
该方法写入直接写入列表元素
2.str转化为字符串写入
l=["A","B","C","D",1,2,3] f=open("k.txt","w") f.write(str(l)) f.close()
该方法会将整个列表转化为字符串并写入,
3.for循环写入
l=["A","B","C","D"] f=open("k.txt","w") for line in l: f.write(line+'\n')
f.close()
此方法的好处是,每个元素都是单词写入,可以在写入时对每个元素进行修改,并且可以换行。但注意
列表中的数字是没有办法与你所要添加的字符相加的(int类型不能与str相加)
4.使用.join函数修改列表
l=["A","B","C","D"] str = '\n' f=open("k.txt","w") f.write(str.join(l))
f.close()
使用join可以很好的添加你想要添加的字符以及数字,非常的灵活,推荐使用,如果列表中存在数值类型,可以先用str转换之后再进行写入
读取数据
1read
如果使用read来读取,则返回为字符串类型,并且列表只能为字符串类型(str),不能存在数值类型,如果有数字则会报错
readline与read相同 ,
如果使用readlines来读取,则默认返回为列表类型,并且列表的一个元素为一行元素
这时便可以对数据进行很好的处理
读取列表中的数字,提供一个简单的方法
for line in l: if "0" in next or "1" in next: