2016-11-24 61 views
0

在csv文件中,如果行以#號開頭或者它是空的,我可以很容易地刪除或忽略它。csv文件開始之前的幾個空格在python之前的頭文件數據

# some description here 
# 1 is for good , 2 is bad and 3 for worse 
empty line 

我可以通過忽略空行和行開始#通過跟隨Python中的邏輯。

while True: 
    if len(data[0]) == 0 or data[0][0][0] == '#': 
     data.pop(0) 
    else: 
     break 
return data 

但下面是頭數據,但是它在開始幾個空的空間,然後數據可用

  0 temp_data 1 temp_flow 2 temp_record 3 temp_all 
22   33   434   344 
34   43   434   355 

在一些文件,我得到了標題數據如下圖所示,然後我不得不忽視僅#符號而不是列名

#0 temp_data 1 temp_flow 2 temp_record 3 temp_all 
22   33   434   344 
34   43   434   355 


But I get no clue how to deal with these two situation. 

如果有人幫助我。它將不勝感激。因爲我的上述邏輯在這兩種情況下失敗了。

+0

您可以編輯輸入文件,以對您的簡歷文件的Fileds的想法? –

+1

@ nexus66我真的很感謝你的幫助。 。 – Rio

+1

謝謝。我更新了我的答案。測試它並留下您的反饋。 –

回答

0

您可以使用字符串strip()函數刪除龍頭和第一尾空白......

>>> '   0 temp_data 1 temp_flow 2 temp_record 3 temp_all'.strip() 
'0 temp_data 1 temp_flow 2 temp_record 3 temp_all' 
+0

我知道這個功能,但在這裏不適用。 – Rio

相關問題