Python:如何将csv文件相对于第一列以外的列进行切片?

Python:如何将csv文件相对于第一列以外的列进行切片?,python,csv,numpy,slice,Python,Csv,Numpy,Slice,我有一个csv文件,其中显示了许多列和将近500000行。我需要根据显示年份的第二列对该文件进行切片,并保留所有其他列: COL1 COL2 COL3 COL4 COL5 COL6 COL7 xxx 1986 xxx xxx xxx xxx xxx xxx 1992 xxx xxx xxx xxx xxx xxx 1998 xxx xxx xxx xxx xxx ...

我有一个
csv
文件,其中显示了许多列和将近500000行。我需要根据显示年份的第二列对该文件进行切片,并保留所有其他列:

COL1   COL2   COL3   COL4   COL5   COL6   COL7
xxx    1986   xxx    xxx    xxx    xxx    xxx
xxx    1992   xxx    xxx    xxx    xxx    xxx
xxx    1998   xxx    xxx    xxx    xxx    xxx
...    ...    ...    ...    ...    ...    ...
xxx    2015   xxx    xxx    xxx    xxx    xxx
xxx    1984   xxx    xxx    xxx    xxx    xxx
我的问题:如果第二列中的值为
=1992
,如何从中生成另一个
csv
文件

期望输出:

COL1   COL2   COL3   COL4   COL5   COL6   COL7
xxx    1992   xxx    xxx    xxx    xxx    xxx
xxx    1998   xxx    xxx    xxx    xxx    xxx
xxx    2015   xxx    xxx    xxx    xxx    xxx
我的尝试是这样的,但我在插入链接到第二列的
时遇到了困难,但我不知道如何做:

from __future__ import division
import numpy
from numpy import *
import csv
from collections import *
import os
import glob

directoryPath=raw_input('Working directory: ') #Indicates where the csv file is located
for i,file in enumerate(os.listdir(directoryPath)): #Loops over the folder where the csv files are
    if file.endswith(".csv"): #Checks if they are csv files
        filename=os.path.basename(file) #Takes the complete path to the file
        filelabel=file #Takes the filename only
        strPath = os.path.join(directoryPath, file) #Retrieves the complete path to find the csv file
        x=numpy.genfromtxt(strPath, delimiter=',')[:,7] #I GOT STUCK HERE

您可以迭代CSV的行,查看COL2中的值是否大于等于您感兴趣的年份。如果是,只需将该行添加到新列表中即可。将新列表传递给CSV编写器。您可以在循环中调用该函数,为以
csv
扩展名结尾的所有文件创建新的csv

您必须通过
工作目录
年份
。这是您要处理的CSV文件夹

import csv
import os
def make_csv(in_file, out_file, year):
    with open(in_file, 'rb') as csv_in_file:
        csv_row_list = []
        first_row = True
        csv_reader = csv.reader(csv_in_file)
        for row in csv_reader:
            if first_row:
                csv_row_list.append(row)
                first_row = False
            else:
                if int(row[1]) >= year:
                    csv_row_list.append(row)

    with open(out_file, 'wb') as csv_out_file:
        csv_writer = csv.writer(csv_out_file)
        csv_writer.writerows(csv_row_list)

for root, directories, files in os.walk(working_directory):
    for f in files:
        if f.endswith('.csv'):
            in_file = os.path.join(root, f)
            out_file = os.path.join(root, os.path.splitext(f)[0] + '_new' + os.path.splitext(f)[1])
            make_csv(in_file, out_file, year)

我试图为脚本分配一个工作目录,其中包含所有csv文件,以便脚本一次性运行。我已经使用以下第一行将def函数嵌套在for循环中:
for I,file in enumerate(os.listdir(directoryPath)):
if file.endswith(.csv”):
year=int(原始输入('Enter the slicing year:'),
in_file=file
out_file=file[0:17]+“u slicied”+str(year file[17:21]),然后是def函数。但是抛出了一个错误:
[Errno 2]没有这样的文件或目录:
+要处理的现有文件的名称。。。发生了什么事?我修改了这个示例来演示如何在循环中调用函数。好的,谢谢。它可以工作,除了如果我有带数据而不带标题的行0,我会直接在切片文件中获得该行,即使不遵守
=year
。如果我有标题就可以了,但如果我没有呢?测试第一行是数据还是标题。也许可以测试第0行的第[1]行是整数(这意味着它是一年)还是字符串(这意味着它是一个标题)。