我正在使用boto3
和Python
调用AWS API,我正在向JSON
文件写JSON
响应。然后我尝试将JSON
文件转换为CSV
文件。当我尝试使用csv writer()
方法执行此操作时,出现上述错误,我不确定原因。
代码:
def ResponseConvert():
dynamo = boto3.client('dynamodb')
response = dynamo.scan(
TableName='XXXX'
)
with open('vuln_data.json', 'w') as outfile:
json.dump(response, outfile, indent=4)
f = open('vuln_data.json')
data = json.load(f)
f.close()
f = csv.writer(open('vuln_data.csv', 'wb+'))
f.writerow(data.keys())
for row in data:
f.writerow(row.values())
ResponseConvert()
回溯:
Traceback (most recent call last):
File "response_convert.py", line 21, in <module>
ResponseConvert()
File "response_convert.py", line 19, in ResponseConvert
f.writerow(row.values())
AttributeError: 'unicode' object has no attribute 'values'
答案 0 :(得分:1)
CSV写入程序需要文件句柄,而不是文件名。
with open('filename.csv', 'w') as f:
writer = csv.writer(f)
...
顺便说一下,你可能想要一个DictWriter
。不要依赖keys
和values
匹配的顺序。