下载谷歌电子表格到csvcsv.writer文件在每个ch之后添加分隔符

2024-04-28 09:35:43 发布

您现在位置:Python中文网/ 问答频道 /正文

从这里开始编写代码:https://gist.github.com/cspickert/1650271而不是printing,我想写入csv文件。你知道吗

在底部添加:

# Request a file-like object containing the spreadsheet's contents
csv_file = gs.download(ss)

# Write CSV object to a file
with open('test.csv', 'wb') as fp:
    a = csv.writer(fp, delimiter=',')
    a.writerows(csv_file)

也许我需要先转换成csv文件然后才能写?你知道吗


Tags: 文件csv代码httpsgithubcomobjectrequest
1条回答
网友
1楼 · 发布于 2024-04-28 09:35:43

Documentation说:

csvwriter.writerows(rows) Write all the rows parameters (a list of row objects as described above) to the writer’s file object, formatted according to the current dialect.

由于csv文件是类似文件的对象,因此需要将其转换为行列表:

rows = csv.reader(csv_file)
a.writerows(rows)

或者,更好的是,您可以简单地写入文件:

csv_file = gs.download(ss)
with open('test.csv', 'wb') as fp:
    fp.write(csv_file.read())

相关问题 更多 >