Django将JSON转换为CSV [英] Django convert JSON to CSV
问题描述
我正在研究一种从Data SF加载数据并将其下载为CSV的功能.我可以查询他们的数据库,并在模板中准确显示结果,但是我无法将这些结果写入CSV.我一直在网上阅读有关将JSON转换为CSV的问题,但似乎无法将它们连接到我的项目.
此视图中的代码将进行查询并在模板中显示结果:
if request.POST:
qAddress = request.POST.get('qAddress')
url = 'https://data.sfgov.org/resource/2zah-tuvt.json?qAddress=%s' % qAddress
search_request = requests.get(url, verify=False)
search_results = search_request.json()
args = {'url': url, 'search_results': search_results}
args.update(csrf(request))
return render(request, 'sts_soda_search_results.html', args)
但是,此代码失败:
if request.POST:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="Search Results.csv"'
url = request.POST.get('url')
search_request = requests.get(url, verify=False)
search_results = search_request.json()
writer = csv.writer(response)
writer.writerow(['Tree ID', 'Species', 'DBH', 'Address', 'Latitude', 'Longitude'])
for obj in search_results:
writer.writerow([obj.treeid, obj.qspecies, obj.dbh, obj.qaddress, obj.latitude, obj.longitude])
return response
我反而收到此错误: 'dict'对象没有属性'treeid'
使用writer.writeror(obj.values())替换writer.writerow([obj.treeid ....])是可行的,但是由于某些查询具有我不想包括的其他字段,因此不一致./p>
json.loads(),json.load()和json.dumps()不返回查询,因此有一种方法可以使它与.json()一起使用并将结果转换为实例csv writer可以处理吗?我对为什么它可以在模板中而不在csv writer中起作用感到困惑.
在我对原始问题的评论中,由BorrajaX和Mad Wombat回答,问题是我不正确地引用了dict键.工作代码:
if request.POST:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="Search Results.csv"'
url = request.POST.get('url')
search_request = requests.get(url, verify=False)
search_results = search_request.json()
writer = csv.writer(response)
writer.writerow(['Tree ID', 'Species', 'DBH', 'Address', 'Latitude', 'Longitude'])
for obj in search_results:
writer.writerow([obj['treeid'], obj['qspecies'], obj['dbh'], obj['qaddress'], obj['latitude'], obj['longitude']])
return response
I am working on a function to load data from Data SF and download it as a CSV. I can make a query to their database and accurately display the results in a template, but I cannot write those results to a CSV. I've been reading online about issues converting JSON to CSV, but I can't seem to connect them to my project.
The code from this view will make a query and display the results in a template:
if request.POST:
qAddress = request.POST.get('qAddress')
url = 'https://data.sfgov.org/resource/2zah-tuvt.json?qAddress=%s' % qAddress
search_request = requests.get(url, verify=False)
search_results = search_request.json()
args = {'url': url, 'search_results': search_results}
args.update(csrf(request))
return render(request, 'sts_soda_search_results.html', args)
However, this code fails:
if request.POST:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="Search Results.csv"'
url = request.POST.get('url')
search_request = requests.get(url, verify=False)
search_results = search_request.json()
writer = csv.writer(response)
writer.writerow(['Tree ID', 'Species', 'DBH', 'Address', 'Latitude', 'Longitude'])
for obj in search_results:
writer.writerow([obj.treeid, obj.qspecies, obj.dbh, obj.qaddress, obj.latitude, obj.longitude])
return response
I instead get this error: 'dict' object has no attribute 'treeid'
replacing writer.writerow([obj.treeid....]) with writer.writeror(obj.values()) works, but is inconsistent since some queries have additional fields that I don't want to include.
json.loads(), json.load() and json.dumps() don't return the query, so is there a way to make it work with using .json() and convert the results to an instance that csv writer can process? I'm confused as to why it works in a template but not in csv writer.
Answered by BorrajaX and Mad Wombat in the comments to my original question, the problem was that I was incorrectly referring to the dict keys. The working code:
if request.POST:
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment; filename="Search Results.csv"'
url = request.POST.get('url')
search_request = requests.get(url, verify=False)
search_results = search_request.json()
writer = csv.writer(response)
writer.writerow(['Tree ID', 'Species', 'DBH', 'Address', 'Latitude', 'Longitude'])
for obj in search_results:
writer.writerow([obj['treeid'], obj['qspecies'], obj['dbh'], obj['qaddress'], obj['latitude'], obj['longitude']])
return response
这篇关于Django将JSON转换为CSV的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!