13.Python去掉行头部尾部选取连续的行

1.基础python实现

#使用row_counter变量来跟踪行编号

vi read_select_contiguous_rows.py 
#!/usr/bin/env python3 
import sys 
import csv 
input_file=sys.argv[1]
output_file=sys.argv[2]
row_counter=0
with open(input_file,'rb') as csv_in_file:
	with open(output_file,'wb') as csv_out_file:
		filereader=csv.reader(csv_in_file)
		filewriter=csv.writer(csv_out_file)
		for row in filereader:
			if row_counter>=3 and row_counter<=15:   #4-16行写入输出文件
				filewriter.writerow([value.strip() for value in row])
			row_counter+=1 

#结果 
[root@mysql51 python_scripts]# cat supplier_data_unnecessary_header_footer.csv
Idon't care about this row,,,,
Idon't care about this row,,,,
Idon't care about this row,,,,
Supplier Name,Invoice Number,Part Number,Cost,Purchase Date
Supplier x,001-1001,2341,$500 ,1/20/2014
Supplier x,001-1001,2341,$501 ,1/20/2014
Supplier x,001-1001,5467,$750 ,1/20/2014
Supplier x,001-1001,5467,$750 ,1/20/2014
Supplier y,50-9501,7009,$250 ,1/30/2014
Supplier y,50-9501,7009,$250 ,1/30/2014
Supplier y,50-9505,6650,$125 ,2/3/2014
Supplier y,50-9505,6650,$125 ,2/3/2014
Supplier z,920-4803,3321,$615 ,2/3/2014
Supplier z,920-4804,3321,$615 ,2/10/2014
Supplier z,920-4805,3321,$615 ,2/17/2014
Supplier z,920-4806,3321,$615 ,2/24/2014
Idon't care about this row either,,,,
Idon't care about this row either,,,,
Idon't care about this row either,,,,
			
[root@mysql51 python_scripts]# python read_select_contiguous_rows.py supplier_data_unnecessary_header_footer.csv 11output.csv 
[root@mysql51 python_scripts]# more 11output.csv
Supplier Name,Invoice Number,Part Number,Cost,Purchase Date
Supplier x,001-1001,2341,$500,1/20/2014
Supplier x,001-1001,2341,$501,1/20/2014
Supplier x,001-1001,5467,$750,1/20/2014
Supplier x,001-1001,5467,$750,1/20/2014
Supplier y,50-9501,7009,$250,1/30/2014
Supplier y,50-9501,7009,$250,1/30/2014
Supplier y,50-9505,6650,$125,2/3/2014
Supplier y,50-9505,6650,$125,2/3/2014
Supplier z,920-4803,3321,$615,2/3/2014
Supplier z,920-4804,3321,$615,2/10/2014
Supplier z,920-4805,3321,$615,2/17/2014
Supplier z,920-4806,3321,$615,2/24/2014

#可以发现前面和后面的语句都被删除。仅保留了中间部分。

2.通过pandas的方法实现。

vi read_select_contiguous_rows.py 
#!/usr/bin/env python3 
import pandas as pd 
import sys 
input_file=sys.argv[1]
output_file=sys.argv[2]
data_frame=pd.read_csv(input_file,header=None) #header=none,表头未知。
data_frame=data_frame.drop([0,1,2,16,17,18])
data_frame.columns=data_frame.iloc[0]
data_frame=data_frame.reindex(data_frame.index.drop(3))
data_frame.to_csv(output_file,index=False)


python C:\Users\4201.HJSC\PycharmProjects\pythonProject\read_select_contiguous_rows.py \
C:\Users\4201.HJSC\Desktop\Python_exercise\supplier_data_unnecessary_header_footer.csv \
C:\Users\4201.HJSC\Desktop\Python_exercise\11output.csv

more 11output.csv
Supplier Name,Invoice Number,Part Number,Cost,Purchase Date
Supplier x,001-1001,2341,$500 ,1/20/2014
Supplier x,001-1001,2341,$501 ,1/20/2014
Supplier x,001-1001,5467,$750 ,1/20/2014
Supplier x,001-1001,5467,$750 ,1/20/2014
Supplier y,50-9501,7009,$250 ,1/30/2014
Supplier y,50-9501,7009,$250 ,1/30/2014
Supplier y,50-9505,6650,$125 ,2/3/2014
Supplier y,50-9505,6650,$125 ,2/3/2014
Supplier z,920-4803,3321,$615 ,2/3/2014
Supplier z,920-4804,3321,$615 ,2/10/2014
Supplier z,920-4805,3321,$615 ,2/17/2014
Supplier z,920-4806,3321,$615 ,2/24/2014

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值