简体   繁体   English

如何在特定文件夹上创建一个空的 csv 文件?

[英]How do I create an empty csv file on a specific folder?

I had a doubt on how to create an empty csv file where I can open it later to save some data using python.我对如何创建一个空的 csv 文件有疑问,稍后我可以在其中打开它以使用 python 保存一些数据。 How do I do it?我该怎么做? Thanks谢谢

An empty csv file can be created like any other file - you just have to perform any operation on it.可以像创建任何其他文件一样创建一个空的 csv 文件 - 您只需对其执行任何操作。 With bash, you can do touch /path/to/my/file.csv .使用 bash,您可以执行touch /path/to/my/file.csv

Note that you do not have to create an empty file for python to write in. Python will do so automatically if you write to a non-existing file.请注意,您不必为 Python 创建一个空文件来写入。如果您写入一个不存在的文件,Python 将自动执行此操作。 In fact, creating an empty file from within python means to open it for writing, but not writing anything to it.实际上,在 python 中创建一个空文件意味着打开它进行写入,但不向其中写入任何内容。

with open("foo.csv", "w") as my_empty_csv:
  # now you have an empty file already
  pass  # or write something to it already

you can also use Pandas to do the same as below:您还可以使用 Pandas 执行以下操作:

import pandas as pd
df = pd.DataFrame(list())
df.to_csv('empty_csv.csv')

After creating above file you can Edit exported file as per your requirement.创建上述文件后,您可以根据需要编辑导出的文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM