2017-04-25 33 views
0

我是熊貓的新用戶,我需要一些幫助。 我有一個文件夾中的10個TXT文件:檢查文本文件中的數據幀是否在輸入前爲空

file_date1_1.txt 
... 
file_date1_5.txt 
file_date2_1.txt 
... 
file_date2_5.txt 

每個文件具有相同的設計:

- text to explain the file 
- datas. 

要導出一個文件,我做了下面的代碼:

Data_Frame = pd.read_csv(Location, delimiter=r'\s+', index_col=False, header = None, skiprows=11) 

什麼我想要做的是做一個循環來製作一個數據框列表。 所以我這樣做:

for i in range(0,len(files_SE)): 
    date.append(files_SE[i][0:7]) 
    hour.append(files_SE[i][8:13]) 
    Location.append('\\'.join([adress,files_SE[i]])) 
    SE_df.append(pd.read_csv(Location[i], delimiter=r'\s+', index_col=False, header = None, skiprows=11)) 

(skiprows是爲了避免DATAS之前的文本和files_SE與所有名的文件列表)。

但我的問題是,循環停止在f ile_date1_5.txt因爲沒有數據(它是空的)。 我想是什麼力量讓一個條件,如

if (pd.read_csv(Location[i], delimiter=r'\s+', index_col=False, header = None, skiprows=11)).empty: 
    *do nothing* 
else: 
    *do the importation of the dataframe* 

沒有人有我一個解決方案嗎? 非常感謝

+0

你可以圍繞'pd.read_csv'用''try'聲明except',只是忽略了的異常,以跳過的文件那是無法讀取的。 – languitar

回答

1

你可以這樣做的檢查:

Current_Data = pd.read_csv(Location[i], delimiter=r'\s+', index_col=False, header = None, skiprows=11) 
if not Current_Data.empty: 
    SE_df.append(Current_Data) 
+0

好的,謝謝,我會嘗試 – Nathan