2017-04-25 79 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