题目一
1.用户通过循环的方式输入5个字符串
把这5个数据作为一行保存到txt文档中,中间用逗号分隔
table=''
f = open('zy1.txt','a',encoding='utf8')
for x in range(1,6):
data=input('请输入一个字符串')
f.write(data)
f.write(',')
table += data
f.close()
题目二
Data.txt中有一行数据,admin,aaa111,unlock,0
1.通过txt文件读的方式,将这一行数据读取出来
2.将读取出来的数据转化为[‘admin’,’aaa111’,’unlock’,0]
f = open('Data.txt', 'r', encoding='utf8')
data=f.readlines()
users = {}
for x in data:
tmp = x.strip('\n').split(',')
a=tmp[0]
b=tmp[1]
c=tmp[2]
d=int(tmp[3])
users=[a,b,c,d]
print(users)
题目三
Data.txt中有两行数据,第一行:admin,aaa111,unlock,0 第二行:tester,aaabbb,unlock,2
1.通过txt的readlines()读方法,将这两行数据读出来
2.将读取出来的数据转化为[[‘admin’,’aaa111’,’unlock’,0], [‘tester’,’aaabbb’,’unlock’,2]]
3.将读取出来的数据转化为:{‘admin’:[ ’aaa111’,’unlock’,0],’tester’:[ ’aaabbb’,’unlock’,2]}
f = open('Data.txt', 'r', encoding='utf8')
data = f.readlines()
users = {}
users1 = {}
for x in data:
tmp = x.split(',')
a=tmp[0]
b=tmp[1]
c=tmp[2]
d=int(tmp[3])
users=[a,b,c,d]
print(users)
for ss in users:
tmp = x.split(',')
a = tmp[0]
b = tmp[1]
c = tmp[2]
d = int(tmp[3])
users1[a] = [b, c, d]
print(users1)
题目四
有一个数据data = {‘admin’:[ ’aaa111’,’unlock’,0],’tester’:[ ’aaabbb’,’unlock’,2]}
1.要将这个数据转化为字符串 tmp = ‘admin,aaa111,unlock,0\n,tester,aaabbb,unlock,2\n’
2.再将这个字符串写入(w模式)到result.txt中
data = {'admin':['aaa111','unlock',0],'tester':['aaabbb','unlock',2]}
data1=""
for x in data.keys():
a0=x
a1=data[x][0]
a2=data[x][1]
a3=data[x][2]
temp=f'{a0},{a1},{a2},{a3}\n'
data1 += temp
print(data1)
f=open('result.txt','w',encoding='utf8')
f.write(data1)
f.close()
题目五
1.通过openpyxl的方式将第2行的内容读取出来,输出的格式为:{‘admin’:[‘aaa111’,’unlock’,0]}
import openpyxl
file = openpyxl.load_workbook('new.xlsx')
sheet = file['name']
users=[]
for i in range(1,5):
data = sheet.cell(row=2,column=i).value
users.append(data)
print(users)
sss={}
for x in users:
name = users[0]
pwd = users[1]
status = users[2]
count = int(users[3])
sss[name] = [pwd, status, count]
print(sss)
file.close()
2.将excel中的第1列读取出来,输出格式为:[‘admin’,’tester’,’qianli’]。注:最大行号:maxrow=sheet.max_row可以得最大行数
import openpyxl
file = openpyxl.load_workbook('new.xlsx')
sheet = file['name']
users=[]
for i in range(1,4):
data = sheet.cell(row=i+1,column=1).value
users.append(data)
print(users)