首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用python使用文件名信息对CSV进行批处理插入列

使用python使用文件名信息对CSV进行批处理插入列
EN

Stack Overflow用户
提问于 2020-08-11 09:56:45
回答 2查看 140关注 0票数 0

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

文件名截图

这些文件的名称如下:

  • 2019年-v-1
  • 2019年-v-2
  • 2019年-v-3
  • 等。

对于每个CSV,我想添加一个列,其中包含标题“访问”,并从文件名(第二个破折号后面的数字)获取该列中的值。

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

如果有Python解决方案,那就太棒了。我不是来自编码背景,这是我唯一熟悉的语言,但我自己似乎无法理解这一语言。

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

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2020-08-11 10:33:10

代码语言:javascript
复制
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路径格式。我想应该会很好的。

票数 1
EN

Stack Overflow用户

发布于 2020-08-11 10:33:25

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

代码语言:javascript
复制
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))]

然后要打开每个文件,添加列,然后再次保存。

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63355798

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档