使用 glob 写入多个 csv

Write to multiple csv using glob

我正在处理大量 csv 文件并且需要添加列。我尝试了 glob,例如:

import glob

filenames = sorted(glob.glob('./DATA1/*2018*.csv'))
filenames = filenames[0:10]

import numpy as np
import pandas as pd

for f in filenames:
    df = pd.read_csv(f, header=None, index_col=None)
    df.columns = ['Date','Signal','Data','Code']
 #this is what I should add to all csv files   
    df["ID"] = df["Data"].str.slice(0,2) 

并且我需要一种方法将文件保存回 csv(未连接),并在将列添加到每个 csv 文件后使用不同的名称(例如 "file01edited.csv")。

使用 to_csvf-strings 来更改文件名:

for f in filenames:
    df = pd.read_csv(f, names=['Date','Signal','Data','Code'], index_col=None)
 #this is what I should add to all csv files   
    df["ID"] = df["Data"].str.slice(0,2) 
    #python 3.6+
    df.to_csv(f'{f[:-4]}edited.csv', index=False)
    #python bellow 3.6
    #df.to_csv('{}edited.csv'.format(f[:-4]), index=False)