練習1:請將user中的元素根據 _ 連結,並寫入 'a1.txt' 的檔案
"""
user = ['alex','eric']
data = "_".join(user)
file_object = open('a1.txt',mode='w',encoding='utf-8')
file_object.write(data)
file_object.close()
"""
練習2:請將user中的元素根據 _ 連結,並寫入 'a1.txt' 的檔案
"""
user = [
{'name':'alex','pwd':'123'}, # alex|123
{'name':'eric','pwd':'olbody'}, # eric|olbody
]
file_object = open('a2.txt',mode='w',encoding='utf-8')
for item in user:
line = "%s|%s\n" %(item['name'],item['pwd'],)
file_object.write(line)
file_object.close()
"""
練習3:請將a2.txt中的檔案讀取出來並新增到一個列表中 ['alex|123','eric|olbody']
方式一
"""
file_obj = open('a2.txt',mode='r',encoding='utf-8')
content = file_obj.read()
file_obj.close()
content = content.strip()
data_list = content.split('\n')
print(data_list)
"""
"""
result = []
file_obj = open('a2.txt',mode='r',encoding='utf-8')
for line in file_obj:
line = line.strip()
result.append(line)
file_obj.close()
print(result)
"""