一、写文件
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