This is iterating through the main file & write to a new file without
the cell 'Total'
f_original = open(fname, 'r')
f_new = open(fname+'_new.csv', 'w')
#iterate through the lines
for line in f_original:
if not line.startswith('Total'):
f_new.write(line)
f_original.close()
f_new.close()
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({
'A' : [1,2,3,4],
'B' : ['a','b','c','d'],
})
In [3]: df.head()
A B
0 1 a
1 2 b
2 3 c
3 4 d
In [4]: df.drop(df.index[len(df)-1])
A B
0 1 a
1 2 b
2 3 c
f_original = open(fname, 'r')
f_new = open(fname+'_new.csv', 'w')
#iterate through the lines
for line in f_original:
if line.startswith('Total'):
f_new.write(line)
f_original.close()
f_new.close()
您还可以使用pandas库
您可以利用这样一个事实:您知道只有一个值,并且前5个字母是
'Total'
。我只需将不满足这些条件的所有行重写为新文件:相关问题 更多 >
编程相关推荐