python 写文件(笔记)

一、写文件

def write_txt(filepath):
	with open(filepath, 'w') as wf:
        	wf.write('**********\n')
        	wf.write('=======\n')
	wf.close()    	   	
write_txt('data.txt')

内容类型(list、dict)

注意:将list、dict的内容转为str类型
(1)list

def write_txt(filepath):
	allUsers_Relation_list = [[1, 2], [2, 4], [4, 6], ['345', '2344']]
	with open(filepath, 'w') as wf:
    	for relation_list in allUsers_Relation_list:
        	wf.write(str(relation_list[0]) + ' ' + str(relation_list[1]) +'\n')
	wf.close()

(2)dict

def write_txt(filepath):
	allUsers_Info_dict = {256478: {12: [4, 5, 8],14: [5, 5, 5]}, 2566666: {13: [6, 5, 8],19: [5, 6, 5]}}
	with open(filepath, 'w') as wf:
    	for uid, user_infos in allUsers_Info_dict.items():
        	wf.write(str(uid) + ' = ' + str(user_infos) + '\n')   	
   	wf.close() 

二、基础知识(list、dict、队列)

判断列表list、字符串类型为空:

len(对象名)

list

1.形式 :list1 = [value,value1,value2]

list1[index] = value

2.初始化

list1 = []

3.添加内容:
(1)简单

list1.append(6)
list1.append(7)
list1.append(8)     
#结果:[6,7,8]

(2)向list追加子list

relation2 = [[156, 4889]]
relation1 = []
user1_uid = 1578
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值