我已经构建了一个文件监控应用程序,使用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.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"的自动递增列
您可以使用 UUID:
import uuid
with open("some_csv.csv", 'a', newline='') as fd:
fd.write(str(uuid.uuid1())
fd.write(file_name)
fd.write(file_size)
fd.write(file_creation)
fd.write(number_columns)