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