使用python使用文件名信息将列批插入CSV

使用python使用文件名信息将列批插入CSV,python,csv,Python,Csv,我有169个CSV文件,具有相同的结构(90列具有相同的标题)和相同的命名系统 这些文件的名称如下: 2019-v-1 2019-v-2 2019-v-3 等等 对于每个CSV,我想添加一列,标题为“访问”,该列中的值取自文件名(末尾的数字,在第二个破折号之后) 因此,例如,第一个CSV将有一个名为“访问”的新列,其中每一行在该列中都被赋予值“1” 如果有一个Python解决方案,那将是惊人的。我不是一个有编码背景的人,这是我唯一熟悉的语言,但我自己似乎无法理解这一点 任何帮助都将不胜感激

我有169个CSV文件,具有相同的结构(90列具有相同的标题)和相同的命名系统

这些文件的名称如下:

  • 2019-v-1
  • 2019-v-2
  • 2019-v-3
  • 等等
对于每个CSV,我想添加一列,标题为“访问”,该列中的值取自文件名(末尾的数字,在第二个破折号之后)

因此,例如,第一个CSV将有一个名为“访问”的新列,其中每一行在该列中都被赋予值“1”

如果有一个Python解决方案,那将是惊人的。我不是一个有编码背景的人,这是我唯一熟悉的语言,但我自己似乎无法理解这一点

任何帮助都将不胜感激-谢谢

import pandas as pd
import os

def csv_folder_input(path,folder):
    path = os.path.join(path,folder)
    os.chdir(path)
    counter=1
    for filename in os.listdir(path):
        if filename.endswith(".csv"):
             with open(filename, 'r') as csvfile:
             counter=counter+1
             df = pd.read_csv(csvfile)
             df['Visits']=int(filename.split('_')[2].split('.')[0])
             df.to_csv(filename)
       
csv_folder_input(your path name,your folder name)
输入路径名,后跟文件夹名。我可以看到你的文件夹名是2019-v。 在文件夹之前输入适当的路径名,并确保输入了MacOS的正确路径格式。我相信它应该很好用

输入路径名,后跟文件夹名。我可以看到你的文件夹名是2019-v。
在文件夹之前输入适当的路径名,并确保输入了MacOS的正确路径格式。我相信它应该可以正常工作。

首先,您需要一个文件列表:

from os import listdir
from os.path import isfile, join
import csv       # You'll need this for the next step

mypath = 'path/to/csv/directory'
allfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
然后要打开每个文件,添加列,然后再次保存

from os import listdir
from os.path import isfile, join
import csv

mypath = 'path/to/csv/directory'
allfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

for file in allfiles:

  # This will only work if your filenames are consistent.
  # We split the name into a list, ['2019', 'v', '1.csv']
  #   then take the third item (index 2), and remove the
  #   last 4 characters.

  number_at_end = file.split("-")[2][:-4]

  # We open the file...

  with open(file, newline='') as csvfile:
    reader = csv.reader(csvfile)

  # Add the column name to the first row, and
  # add the value to each row...

  for i, row in enumerate(reader):
    if i == 0:
      row.append('Visits')
    else:
      row.append(number_at_end)

  # and then write the file back.

  with open(file, newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(reader)

首先,您需要一个文件列表:

from os import listdir
from os.path import isfile, join
import csv       # You'll need this for the next step

mypath = 'path/to/csv/directory'
allfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
然后要打开每个文件,添加列,然后再次保存

from os import listdir
from os.path import isfile, join
import csv

mypath = 'path/to/csv/directory'
allfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]

for file in allfiles:

  # This will only work if your filenames are consistent.
  # We split the name into a list, ['2019', 'v', '1.csv']
  #   then take the third item (index 2), and remove the
  #   last 4 characters.

  number_at_end = file.split("-")[2][:-4]

  # We open the file...

  with open(file, newline='') as csvfile:
    reader = csv.reader(csvfile)

  # Add the column name to the first row, and
  # add the value to each row...

  for i, row in enumerate(reader):
    if i == 0:
      row.append('Visits')
    else:
      row.append(number_at_end)

  # and then write the file back.

  with open(file, newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerows(reader)

非常感谢你的帮助!这看起来很有希望。我不断收到以下错误:“\u csv.reader”对象不可订阅非常感谢您的帮助!这看起来很有希望。我不断收到以下错误:“\u csv.reader”对象不可订阅非常感谢您提供的解决方案!不是问一个新手问题,而是如何/在哪里添加路径名和文件夹名?我试过了,但出现了语法错误。这是一个函数。所以你要做的是。csv\u folder\u输入(您的路径名、文件夹名)我已编辑了答案,以使您更轻松。非常感谢您抽出时间帮我解决这个问题:)非常感谢您提供的解决方案!不是问一个新手问题,而是如何/在哪里添加路径名和文件夹名?我试过了,但出现了语法错误。这是一个函数。所以你要做的是。csv\u folder\u输入(您的路径名、文件夹名)我已编辑了答案,以使您更轻松。非常感谢您抽出时间在这方面帮助我:)