Python 如何向现有CSV文件添加标题

Python 如何向现有CSV文件添加标题,python,csv,file,Python,Csv,File,我正在制作一个CSV过滤程序,将多个CSV文件过滤成一个CSV文件。现在,我的程序正在选择特定的记录,并对CSV文件中的日期时间进行一些处理。现在一切都很完美 现在我想向csv文件添加标题,因为它现在没有任何标题。我尝试使用pandas(请参见最后几行代码)执行此操作,但收到以下错误:writer.writerow(编码的标签)不支持操作:不可写 my_set = {'AA', 'BB', 'CC'} def filter_row(r): condition_1 = set(row).

我正在制作一个CSV过滤程序,将多个CSV文件过滤成一个CSV文件。现在,我的程序正在选择特定的记录,并对CSV文件中的日期时间进行一些处理。现在一切都很完美

现在我想向csv文件添加标题,因为它现在没有任何标题。我尝试使用pandas(请参见最后几行代码)执行此操作,但收到以下错误:
writer.writerow(编码的标签)不支持操作:不可写

my_set = {'AA', 'BB', 'CC'}

def filter_row(r):
    condition_1 = set(row).intersection(my_set) #<-- selecting the specific words to filter on          
    return condition_1 
      
def timestamp_to_columns(timestamp):
    date_object = datetime.datetime.fromisoformat(timestamp)
    time_tuple = date_object.timetuple()
    return [*time_tuple[:6], int(date_object.utcoffset().seconds / 3600)]

def update_row(row):  
    return [
        *timestamp_to_columns(row[0]),
        *row[1:4], # the other columns we need
        *timestamp_to_columns(row[6]),
      ]

for file in os.listdir(destination):
    file_path = os.path.join(destination, file)
    with open(file_path, 'r') as my_file, open('output.txt', 'w') as outer:
        reader = csv.reader(my_file, delimiter = ',')
        next(reader)
        writer = csv.writer(outer, delimiter = '\t')
        for row in reader: 
            if filter_row(row):
                writer.writerow(update_row(row))
            df = pd.read_csv(my_file,  names = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10']) #<--- adding the headers 
            df.to_csv(my_file, index=False)
my_set={'AA','BB','CC'}
def过滤器_行(右):

条件_1=集合(行)。相交(我的集合)#您是否使用了
writeheader
方法:不,我不理解该方法。。。