我有一个CSV文件,我读作每行的字典列表。我想删除列表中具有''
EmailAddress的所有条目。我试过了:
#!/usr/bin/python
import csv
def import_users(location_of_file):
with open(location_of_file, 'r', newline='', encoding='utf-8-sig') as openfile:
reader = csv.DictReader(openfile)
for row in reader:
yield row
def save_csv(data, location):
with open(location, 'w', newline='', encoding='utf-8-sig') as file:
fieldnames = ['EmailAddress', 'GivenName', 'Surname', 'Company', 'Department']
writer = csv.DictWriter(file, fieldnames=fieldnames)
writer.writeheader()
for item in data:
writer.writerow(item)
if __name__ == '__main__':
users = list(import_users('C:\Temp\Example.csv'))
for user in users:
if user['EmailAddress'] == '':
del user
else:
pass
save_csv(users, 'C:\Temp\Output.csv')
但我的结果仍然是没有电子邮件地址的条目。我做错了什么?
答案 0 :(得分:5)
对正在修改的数据结构进行迭代是不好的做法(并且会导致超级烦人的错误)。因此,您应该制作另一个列表,其中只包含您想要的项目。你可以用循环来完成这个:
users = list(import_users('C:\Temp\Example.csv'))
filtered_users = []
for user in users:
if user['EmailAddress'] == '':
filtered_users.append(user)
save_csv(filtered_users, 'C:\Temp\Output.csv')
或使用pythons过滤功能:
users = list(import_users('C:\Temp\Example.csv'))
filtered_users = filter(lambda user: user.get('EmailAddress') != '', users)
save_csv(filtered_users, 'C:\Temp\Output.csv')
答案 1 :(得分:5)
您不需要在内存中创建列表。您可以改为传递生成器和迭代器:
if __name__ == '__main__':
users = import_users('C:/Temp/Example.csv')
save_csv((user for user in users if user['EmailAddress'] != ''),
'C:/Temp/Output.csv')
答案 2 :(得分:4)
不要在刺激他们的情况下更改列表项目。
相反
<input type="hidden" name="business" value="peoplesroboticsfacilitator@outlook.com">
待办事项
for user in users:
if user['EmailAddress'] == '':
del user
else:
pass
答案 3 :(得分:3)
你可能最好不要制作新的清单而不是删除项目:
users = [user for user in users if user['EmailAddress'] != '']
答案 4 :(得分:2)
以下是使用pandas
的解决方案:
示例数据:
import pandas as pd
#Read csv data
df = pd.read_csv('data.csv')
#Get only the rows having email address
dfo = df[pd.notnull(df['EmailAddress'])]
#Save to a file
dfo.to_csv('output.csv', index=False)
示例输出: