当我尝试在csv文件中填充用户First_Name时,我收到此错误。有人能告诉我我做错了吗
import csv
# Define users
valid_input = False
while not valid_input:
users =raw_input('Number of users: ')
try:
users = range(0,int(users))
valid_input = True
except:
print "Invalid input"
pass
First_Name = ["Test"+str(user) for user in range (1, users+1)]
Last_Name = ["User%s" %user for user in users]
Email_Addresses = []
for user in users:
email= raw_input("Email domain for user %d: " %user)
Email_Addresses.append(Last_Name[user] + email)
Password = ["Password1" for user in users]
Group =["Test" for user in users]
Admin = ["Yes" for user in users]
# open a file for writing.
# open a file for writing.
with open('users.csv', 'wb') as csv_out:
writer = csv.writer(csv_out)
writer.writerows(zip(Email_Addresses, Password, First_Name, Last_Name, Group, Admin))
这是我得到的回溯 Traceback(最近一次调用最后一次): First_Name = [“测试”+ str(用户)范围内的用户(1,用户+ 1)] TypeError:只能将列表(不是“int”)连接到列表
答案 0 :(得分:3)
users
是一个列表:
users = range(0,int(users))
但是您尝试从该列表中创建 new 范围:
First_Name = ["Test"+str(user) for user in range (1, users+1)]
其中users + 1
是尝试将1
添加到列表中。
不要那样做。只需循环现有范围:
First_Name = ["Test"+str(user + 1) for user in users]