Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/321.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 删除一系列csv列_Python_Csv_Python 2.7 - Fatal编程技术网

Python 删除一系列csv列

Python 删除一系列csv列,python,csv,python-2.7,Python,Csv,Python 2.7,我正在尝试创建一个可导入模块,以删除我正在处理的文件中的一系列列,特别是第73-177列。我正在尝试编辑此文件I/o代码,该代码是为基于字段名删除列而编写的。我想修改此代码以删除csv文件中的第73-177列。我需要做些什么来实现这一点 def removeColumns(num1, num2, inputFILE, FileName): inPUTfile = open(inputFILE, 'r') outPUTfile = open(FileName, 'w') l

我正在尝试创建一个可导入模块,以删除我正在处理的文件中的一系列列,特别是第73-177列。我正在尝试编辑此文件I/o代码,该代码是为基于字段名删除列而编写的。我想修改此代码以删除csv文件中的第73-177列。我需要做些什么来实现这一点

def removeColumns(num1, num2, inputFILE, FileName): inPUTfile = open(inputFILE, 'r') outPUTfile = open(FileName, 'w') line = inPUTfile.readline() while line: # Delete Specified columns. First column range number, second column range number (+1) lineList = line.split('\t') removeCOL = "Calendar-Year" i = 0 while lineList[i] != removeCOL: #(linesout?): i = i + 1 lineList.pop(i) #remove these fields from the list.append #write modified fields remove = "\t".join(lineList) outPUTfile.write(line) #write the new field names outfile for line in inPUTfile: #remove field i from each remaining line and write it in the output file &modify input line lineList = line.split( ) #convert to a list lineList.pop(i) #remove fields from the list line = '\t'.join(lineList) line = line + '\n' #add a carriage return to the end of the row outPUTfile.write(line)# Write the modified line in the output file inPUTfile.close() #close the input file outPUTfile.close() #close the output file return outPUTfile print outPUTfile
我知道你问过如何修改原始代码,但在这里,我真的认为更容易理解如何用不同的方式来做。Python有一个有用的模块,可以为您处理大量工作。比如:

import csv

remove_from = 2
remove_to = 5

with open("to_delete.csv", "rb") as fp_in, open("newfile.csv", "wb") as fp_out:
    reader = csv.reader(fp_in, delimiter="\t")
    writer = csv.writer(fp_out, delimiter="\t")
    for row in reader:
        del row[remove_from:remove_to]
        writer.writerow(row)
将转向

$ cat to_delete.csv 
a   b   c   d   e   f   g
1   2   3   4   5   6   7
8   9   10  11  12  13  14
15  16  17  18  19  20  21
进入


我知道你问过如何修改原始代码,但在这里,我真的认为更容易理解如何用不同的方式来做。Python有一个有用的模块,可以为您处理大量工作。比如:

import csv

remove_from = 2
remove_to = 5

with open("to_delete.csv", "rb") as fp_in, open("newfile.csv", "wb") as fp_out:
    reader = csv.reader(fp_in, delimiter="\t")
    writer = csv.writer(fp_out, delimiter="\t")
    for row in reader:
        del row[remove_from:remove_to]
        writer.writerow(row)
将转向

$ cat to_delete.csv 
a   b   c   d   e   f   g
1   2   3   4   5   6   7
8   9   10  11  12  13  14
15  16  17  18  19  20  21
进入


我尝试运行该代码,但不幸的是它没有删除列。我是python脚本编写的初学者,但目标是定义一个过程,从任何csv文件中删除一系列列,然后第一步就是明确理解csv模块。如上所示,代码在以制表符分隔的csv文件上应该可以正常工作;把它变成一个函数很简单。发生了什么事?您收到错误消息了吗?没有错误消息。在python中,它似乎运行得很好,使用的代码与提供的完全相同,但当我打开新的csv文件进行检查时,列仍然在那里。嗯,您必须至少更改了文件名,因此您不能完全按照提供的方式使用它。您能告诉我您的原始文件名.csv.readline提供了什么打印格式吗?这就解决了它!它现在工作得很好!非常感谢你!我尝试运行该代码,但不幸的是它没有删除列。我是python脚本编写的初学者,但目标是定义一个过程,从任何csv文件中删除一系列列,然后第一步就是明确理解csv模块。如上所示,代码在以制表符分隔的csv文件上应该可以正常工作;把它变成一个函数很简单。发生了什么事?您收到错误消息了吗?没有错误消息。在python中,它似乎运行得很好,使用的代码与提供的完全相同,但当我打开新的csv文件进行检查时,列仍然在那里。嗯,您必须至少更改了文件名,因此您不能完全按照提供的方式使用它。您能告诉我您的原始文件名.csv.readline提供了什么打印格式吗?这就解决了它!它现在工作得很好!非常感谢你!