我有一个带有标题的CSV文件。要删除csv的前5行但不删除标题吗?用Python
我有一个下面的CSV文件,其标题为(a、B、C、D): 我希望在删除前5行但不删除标题后输出:我有一个带有标题的CSV文件。要删除csv的前5行但不删除标题吗?用Python,python,python-2.7,csv,Python,Python 2.7,Csv,我有一个下面的CSV文件,其标题为(a、B、C、D): 我希望在删除前5行但不删除标题后输出: A,B,C,D 1,1,7,3 2,9,5,6 下面是我的Python代码片段,但我无法添加任何保留头的代码: 打开(filename.csv,'rb')作为内嵌: data_in=infle.readlines() 以open('temp.csv','wb')作为输出文件: outfile.writelines(数据在[5:]中) 请帮助我。在我的情况下,页眉也在删除,但我希望每次都保留页眉。如何
A,B,C,D
1,1,7,3
2,9,5,6
下面是我的Python代码片段,但我无法添加任何保留头的代码:
打开(filename.csv,'rb')作为内嵌:
data_in=infle.readlines()
以open('temp.csv','wb')作为输出文件:
outfile.writelines(数据在[5:]中)
请帮助我。在我的情况下,页眉也在删除,但我希望每次都保留页眉。如何:
with open ('temp.csv', 'wb') as outfile:
outfile.writelines(data_in[0])
outfile.writelines(data_in[5:])
我建议使用熊猫,因为它将保留标题,您可以执行
轻松地对数据执行多种操作。熊猫数据框可以以类似于csv文件的列和行的形式表示二维数据
将文件加载到数据帧中
df = pd.read_csv('file.csv')
然后选择所需的行
df_temp = df.loc[5:]
这里是必需的输出
A B C D
5 1 1 7 3
6 2 9 5 6
您可以进一步将其写入csv文件
df_temp.to_csv('output.csv',index=False)
您可以使用以避免将整个文件读入内存:
from itertools import islice
import csv
with open('input.csv', 'rb') as f_input, open('output.csv', 'wb') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
csv_output.writerow(next(csv_input))
csv_output.writerows(islice(csv_input, 5, None))
为您提供以下输出:
A,B,C,D
1,1,7,3
2,9,5,6
这首先读取第一行并将其写入输出。然后它使用
islice()
跳过5行,然后将剩余的行传递给writerows()
我建议甚至不要解析文件或在内存中读取整个文件来对其进行切片。如果只想在中间删除一些行,则需要逐行读取输入文件,并决定要写入输出文件的行,哪些行要跳过:
skip_lines = range(1, 6) # the range is zero-indexed
with open("input.csv") as f_in, open("output.csv", "w") as f_out:
current_line = 0 # keep a line counter
for line in f_in: # read the input file line by line
if current_line not in skip_lines:
f_out.write(line) # not in our skip range, write the line
current_line += 1 # increase the line counter
我建议使用csv.DictReader和csv.DictWriter:
filename = os.path.join(datapath, "input.csv")
with open(filename, 'rb') as infile:
reader = csv.DictReader(infile)
data_in = [row for row in reader]
fieldnames = reader.fieldnames
filename = os.path.join(datapath, "temp.csv")
with open(filename, 'wb') as outfile:
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data_in[5:])
简单的解决方案:使用两个
outfile.writelines()
命令,一个用于[0]中的data\u,然后用于[5:
中的data\u。谢谢@VBB,它起了作用
filename = os.path.join(datapath, "input.csv")
with open(filename, 'rb') as infile:
reader = csv.DictReader(infile)
data_in = [row for row in reader]
fieldnames = reader.fieldnames
filename = os.path.join(datapath, "temp.csv")
with open(filename, 'wb') as outfile:
writer = csv.DictWriter(outfile, fieldnames=fieldnames)
writer.writeheader()
writer.writerows(data_in[5:])