【发布时间】:2020-02-22 22:30:17
【问题描述】:
我正在通过 CSV 读取数据并将其中一个值存储到列表中。
当我尝试比较值时,结果发现列表迭代了一项。如果我打印列表,我可以看到列表的全部内容。但是,如果我在 for 循环 (for i in list:) 之后打印,我只会得到第一项,列表中有多少次,但总是相同的值。
def getMail(fname, lname):
firstname = fname.split()
lastname = lname.split()
email = firstname[0].strip() + '.' + lastname[0].strip() + '@test.com'
e_mails=[]
with open('test-input.txt','r') as AD:
rows = csv.DictReader(AD)
for ad in rows:
e_mails.append(ad['UserLogonName'])
for i in e_mails:
print(f'{i} and {email}')
'''
This is what i get when I print i and email. i expecxted i to be
an item on the list is stored.
carlos.jimenez@test.com and Apolinar.Vargas-Morales@test.com
carlos.jimenez@test.com and Antonio.Ochoa@test.com
carlos.jimenez@test.com and Steven.Diaz@test.com
carlos.jimenez@test.com and Jorge.Medrano@test.com
carlos.jimenez@test.com and Daniel.Cerano@test.com
'''
print(e_mails)
'''
this is the content of my list from CSV
['carlos.jimenez@test.com', 'Carri.Dix@test.com', 'Alena.Azizi@test.com', 'Berna.Lagonaso@test.com', 'Christophe.deCarbonnieres@test.com', 'daniel.min@test.com', 'David.Li@test.com', 'John.Mitchell@test.com', 'Naveen.Sisodia@test.com', 'nadia.shakir@test.com', 'Sumeet.Sharma@smurfitkappa.com', 'benedicte.bar@test.com', 'Patricia.Aguirre@test.com']
'''
if email.lower() == i.lower():
# print(f'{i} and {email}')
print(len(firstname))
if firstname ==2:
email = firstname[0]+'.'+firstname[0][0:1]+'.'+lastname[0]+'@test.com'
return email.lower()
elif firstname ==1 :
email = firstname[0]+'.'+firstname[0][0:1]+'.'+lastname[0]+'@test.com'
return email.lower()
else:
return email.lower()
with open('users.csv') as csv_file:
rows = csv.DictReader(csv_file)
with open('users-COI2-Multi.csv', 'w', newline='') as output:
header = ['FirstName','Initials','LastName','DisplayName','Description','Office','TelePhone','UserLogonName','SamAccountName','JobTitle','Department','Manager','Mobile','faxNumber','Notes','Assistant','employeeID','ex1','ex2','ex3','ex15','Office365License','ExpiresInDays','EmailToUSer','AddToGroup']
output_file = csv.DictWriter(output, fieldnames=header, delimiter=';')
output_file.writeheader()
for data in rows:
employeeId = data['Associate ID']
fName = data['First Name']
lName = data['Last Name']
Location = data['Location']
Department = data['Department']
Manager = data['Manager Name']
JobTitle = data['Title']
context = {
'FirstName' : fName,
'Initials' : getInitials(fName, lName),
'LastName' : lName,
'DisplayName' : getDisplayName(fName, lName),
'Description' : 'LMS Account for: '+getDisplayName(fName, lName),
'Office': getOffice(Location).strip(),
'TelePhone' : '+1 XXX XXX XXX',
'UserLogonName' : getMail(fName, lName),
'SamAccountName' : getSamA(fName, lName),
'JobTitle' : JobTitle,
'Department' : Department,
'Manager' : Manager,
'Mobile' : '',
'faxNumber' : '',
'Notes' : '',
'Assistant' : '',
'employeeID' : employeeId,
'ex1' : 'End User',
'ex2' : 'NoMailbox',
'ex3' : getSiteCode(Location),
'ex15' : getSKID(Location),
'Office365License' : '',
'ExpiresInDays' : '',
'EmailToUSer' : 'victor.u.reyes@test.com',
'AddToGroup' : '',
}
output_file.writerow(context)
【问题讨论】:
-
@ThomasWeller 我的代码中有另一个循环是我如何生成输出 CSV
标签: python python-3.x list list-comprehension