Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在每次添加内容时向csv添加序列号?_Python_Python 3.x_Csv_Python Watchdog - Fatal编程技术网

Python 如何在每次添加内容时向csv添加序列号?

Python 如何在每次添加内容时向csv添加序列号?,python,python-3.x,csv,python-watchdog,Python,Python 3.x,Csv,Python Watchdog,我构建了一个文件监控应用程序,用于添加新文件。它会在父目录中的csv中添加有关每个新文件的数据。我现在还想在数据中附加序列号。我该怎么做呢 以下是我现在拥有的: with open("some_csv.csv", 'a', newline='') as fd: # i want to append auto incrementing serial numbers here fd.write(file_name) fd.write(file_size) fd.wri

我构建了一个文件监控应用程序,用于添加新文件。它会在父目录中的csv中添加有关每个新文件的数据。我现在还想在数据中附加序列号。我该怎么做呢

以下是我现在拥有的:

with open("some_csv.csv", 'a', newline='') as fd:
    # i want to append auto incrementing serial numbers here
    fd.write(file_name)
    fd.write(file_size)
    fd.write(file_creation)
    fd.write(number_columns)

如果您对pandas模块没有问题,那么它非常简单

import pandas as pd
df = pd.read_csv('test2.csv', sep=',', header=None)
df.to_csv('test3.csv', index_label='index')

上面的代码将生成一个名为“tes3.csv”的csv,其中包含一个名为“index”的自动递增列。

如果您对pandas模块满意,那么这非常简单

import pandas as pd
df = pd.read_csv('test2.csv', sep=',', header=None)
df.to_csv('test3.csv', index_label='index')
上述代码将生成一个名为“tes3.csv”的csv,其中包含一个名为“index”的自动递增列。

您可以使用:

您可以使用:


我要序列号。不,我要序列号。不是,我根据你的答案制定了一个解决方案,甚至从未想过熊猫会对我有所帮助,可能是因为我想到的解决方案太复杂了。我根据你的答案制定了一个解决方案,甚至从未想过熊猫会对我有所帮助,可能是因为我想到的解决方案太复杂了。