我的问题是如何将CSV文件发送到FTP服务器。如您所见,以下脚本是我当前的代码:
代码示例:
def download_outage_info_all(request):
upload_data = download_data_form(request.POST)
if upload_data.is_valid():
print("valid")
start = upload_data.cleaned_data['start_date_time']
end = upload_data.cleaned_data['end_date_time']
print(start, '-', end)
start_timestamp = datetime.strptime(
start, '%Y-%m-%d %H:%M')
end_timestamp = datetime.strptime(
end, '%Y-%m-%d %H:%M')
try:
info = planned_outages.objects.filter(
start_timestamp__gte=start_timestamp, end_timestamp__lte=end_timestamp).values()
except Exception as e:
print("EXCEPTION", e)
print("**** Data not found *** ")
filename_date_part = datetime.now().strftime("%Y%m%d%H%M")
response = HttpResponse(content_type='text/csv')
response['Content-Disposition'] = 'attachment;filename=m_availability_' +
filename_date_part + '.csv'
writer = csv.writer(response, delimiter=';')
writer.writerow(['starts YYYY-mm-dd HH:MM:SS', 'time_zone',
'ends YYYY-mm-dd HH:MM:SS', 'asset id', 'availability type', 'PowerKW'])
for x in info:
try:
unit_mw = unit_details.objects.get(
unit_id=x['unit_id_id'])
# prints to csv file
writer.writerow([x['start_timestamp'], 'UTC',
x['end_timestamp'], unit_mw.unit_name,x['availability_type'], x['capacity_kw']])
except Exception as e:
print("EXCEPTION", e)
print("**** Data not found for unit_mw*** ")
return response
这是一个Django视图,我不想在我的本地系统上保存CSV,我只是想直接将它发送到FTP服务器。谁能帮我?