如何解决Python读取目录并输出到CSV
我只是试图读取目录,然后将文件名放入csv。目录读取工作正常。当我尝试写入csv时,出现此错误。
第148行,位于_dict_to_list中 错误的字段= rowdict.keys()-self.fieldnames AttributeError:“ str”对象没有属性“键”
import os
import csv
path = 'F:\Production\FocusPoint'
filename = 'storage.csv'
fields = ['Name']
with os.scandir(path) as listOfEntries:
for entry in listOfEntries:
if entry.is_file():
f = (entry.name)
with open(filename,'w',newline='') as csvfile:
writer = csv.DictWriter(csvfile,fieldnames= fields)
writer.writeheader()
writer.writerows(f)
解决方法
您的变量fields
不是字典。因此csv.DictWriter
不起作用。怎么样?
writer = csv.writer(csvfile)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。