【发布时间】:2018-09-19 12:40:22
【问题描述】:
我有一个代码来创建一个 csv 文件并可以将它上传到 ftp 服务器
with open(csv_file, 'w') as csvfile:
writer = csv.DictWriter(csvfile, fieldnames=csv_columns1) #csv_columns1 is a list of value to become as heading
writer.writeheader()
for data in vals['details']:# values for the header
writer.writerow(data)
writer = csv.DictWriter(csvfile, fieldnames=csv_columns2)#csv_columns1 is a list of value to become as heading
writer.writeheader()
writer = csv.DictWriter(csvfile, fieldnames=csv_columns)#csv_columns1 is a list of value to become as heading
writer.writeheader()
for data in vals['OrderLine']:# values for the header
writer.writerow(data)
print(os.system('pwd'))
Output_Directory = "ftp_path_to_store_file"
username = "ftp_user_names"
password = "ftp_password"
ftp_ip = "ftp_host_ip"
a1 = 'STOR %s.csv' % (order.name)
try:
ftp = FTP(ftp_ip)
ftp.login(username, password)
ftp.cwd(Output_Directory)
with open(csv_file, "rb") as f:
ftp.storbinary('STOR ' + os.path.basename(csv_file), f)
但我需要的是不在我的计算机上创建文件我想直接创建一个文件到 ftp 服务器
任何帮助将不胜感激
【问题讨论】:
-
不要将其写入文件...将其写入
io.BytesIO对象...然后将ftp.storbinary该对象... -
我也试过了。但它向我显示了一些错误,例如 writer 对象没有属性 DictWriter。你能告诉我我该怎么做吗?使用上面的代码 csv_column1=[head1, head2,head3] csv_column2=[head21, head22,head23] ..etc
-
为什么要创建多个 CSV 编写器?
-
我有多行标题要添加到 csv 有没有其他方法可以做到这一点
-
这不是 CSV 文件的工作方式...但无论如何...您的代码可能只需将
with open(csv_file, 'w') as csvfile:更改为with io.BytesIO() as csvfile...
标签: python python-3.x file ftp