无法将 csv 文件的 header 小写

Unable to lowercase the header of csv file

我正在尝试使用 python 将目录中的多个 csv 文件中的第一个 row/header 设为小写。代码和错误如下。有什么方法可以修复代码或其他方法吗?

import csv
import glob

path = (r'C:\Users\Documents')

for fname in glob(path):
    with open(fname, newline='') as f:
        reader = csv.reader(f)
        row1 = next(reader)
        for row1 in reader:
            data = [row1.lower() for row1 in row1]
            os.rename(row1, data)

错误是:

TypeError: rename: src should be string, bytes or os.PathLike, not list

我认为您混淆了行和列。我认为这是一些未经测试的代码,可以满足您的需求:

import csv
from glob import glob

path = (r'C:\Users\Documents\*.csv')  # Note wildcard character added for glob().

for fname in glob(path):
    with open(fname, newline='') as f:
        reader = csv.reader(f)
        header = next(reader)  # Get the header row.
        header = [column.lower() for column in header]  # Lowercase the headings.
        rows = [header] + list(reader)  # Read the rest of the rows.

    with open(fname, 'w', newline='') as f:
        writer = csv.writer(f)
        writer.writerows(rows)  # Write new header & original rows back to file.