2016-02-29 170 views

回答

0

你可以利用你知道的事实将只有一个值,前5个字母将是'Total'。我只想改写不符合这些条件的新文件中的所有行:

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() 
+0

为什么不使用'.startswith('Total')'? – wpercy

+0

好得多,我补充说,谢谢。 –

+0

谢谢@LucasCurrah –

0

这是通过主文件&写入到一个新的文件没有迭代的 细胞“合计”

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() 

感谢卢卡斯&威尔伯

0

您还可以使用pandas

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 
相关问题