从csv文件的开头删除行,python

时间:2014-08-08 03:02:59

标签: python file dictionary

说我有以下文件:

student.id, student.name, student.is_male, student.birth_date, student.courseid, student.gradeid 
90, 'Jeff', False, datetime.date(2014, 8, 5), 1, 5
91, 'Allan', True, datetime.date(2014, 8, 5), 1, 1
92, 'Maria', False, datetime.date(2014, 8, 5), 2, 8
93, 'Lucie', False, datetime.date(2014, 8, 5), 5, 7
94, 'John', True, datetime.date(2014, 8, 5), 3, 1
95, 'Pat', False, datetime.date(2014, 8, 5), 4, 2
96, 'Jeff', False, datetime.date(2014, 8, 5), 1, 7
97, 'Allan', True, datetime.date(2014, 8, 5), 4, 6
98, 'Maria', False, datetime.date(2014, 8, 5), 4, 7
99, 'Lucie', False, datetime.date(2014, 8, 5), 5, 1
100, 'John', True, datetime.date(2014, 8, 5), 3, 2
101, 'Pat', False, datetime.date(2014, 8, 5), 3, 5
102, 'Jeff', False, datetime.date(2014, 8, 5), 1, 7
103, 'Allan', True, datetime.date(2014, 8, 5), 4, 5
104, 'Maria', False, datetime.date(2014, 8, 5), 2, 8
105, 'Lucie', False, datetime.date(2014, 8, 5), 5, 9
106, 'John', True, datetime.date(2014, 8, 5), 3, 7
107, 'Pat', False, datetime.date(2014, 8, 5), 2, 8
108, 'Jeff', False, datetime.date(2014, 8, 5), 1, 3
109, 'Allan', True, datetime.date(2014, 8, 5), 1, 9
110, 'Maria', False, datetime.date(2014, 8, 5), 3, 4
111, 'Lucie', False, datetime.date(2014, 8, 5), 1, 4
112, 'John', True, datetime.date(2014, 8, 5), 1, 1
113, 'Pat', False, datetime.date(2014, 8, 5), 3, 8
114, 'Jeff', False, datetime.date(2014, 8, 5), 4, 2
115, 'Allan', True, datetime.date(2014, 8, 5), 2, 5
116, 'Maria', False, datetime.date(2014, 8, 5), 5, 5
117, 'Lucie', False, datetime.date(2014, 8, 5), 3, 8
118, 'John', True, datetime.date(2014, 8, 5), 1, 9
119, 'Pat', False, datetime.date(2014, 8, 5), 5, 8
120, 'Jeff', False, datetime.date(2014, 8, 5), 1, 1
121, 'Allan', True, datetime.date(2014, 8, 5), 4, 3
122, 'Maria', False, datetime.date(2014, 8, 5), 5, 3
123, 'Lucie', False, datetime.date(2014, 8, 5), 1, 5
124, 'John', True, datetime.date(2014, 8, 5), 5, 5
125, 'Pat', False, datetime.date(2014, 8, 5), 5, 1
126, 'Jeff', False, datetime.date(2014, 8, 5), 5, 9
127, 'Allan', True, datetime.date(2014, 8, 5), 5, 6
128, 'Maria', False, datetime.date(2014, 8, 5), 2, 5
129, 'Lucie', False, datetime.date(2014, 8, 5), 1, 2
130, 'John', True, datetime.date(2014, 8, 5), 3, 8

在python中,我想将标题行和前20行读入字典列表,其中:作为每个字典中的键:值对。我想要更新文件内容,保留标题和后续行,同时删除处理过的行。

我该怎么做?

谢谢

1 个答案:

答案 0 :(得分:1)

对于Pandas数据框来说,这是一个完美的工作。无论你想在这里做什么,它都会让你的生活更加轻松。

import pandas as pd
df=pd.read_csv('your.csv')

然后有许多方便(快速)的方法来操纵数据并访问特定的行和索引。

在这里查看文档:

http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.html