f = open('C:\\Users\\del\\Desktop\\新建文本文档 (4).txt','r')
date = f.readlines() #读取所有行,得到一个列表
print(date)
print(date[0])
print(date[1])
print(date[2])
print(date[3])
users = []
for line in date:
user = line[:-1].split(":")
users.append(user)
print(users)
print(users[0][0])
print(users[0][1])
print(users[1][0])
print(users[1][1])
print(users[2][0])
print(users[2][1])
print(users[3][0])
print(users[3][1])
f.close()
f = open('C:\\Users\\del\\Desktop\\新建文本文档 (4).txt','r') date = f.readlines() #读取所有行,得到一个列表 print(date) print(date[0]) print(date[1]) print(date[2]) print(date[3]) users = [] for line in date: user = line[:-1].split(":") users.append(user) print(users) print(users[0][0]) print(users[0][1]) print(users[1][0]) print(users[1][1]) print(users[2][0]) print(users[2][1]) print(users[3][0]) print(users[3][1]) f.close()
执行结果:
[':123\n', 'user:\n', 'error:error\n', 'admin:admin123\n']
:123
user:
error:error
admin:admin123
[['', '123'], ['user', ''], ['error', 'error'], ['admin', 'admin123']]
123
user
error
error
admin
admin123