所以我将3个xlsx文件连接成一个。但有时,其中一个文件可能是空的,而另一个文件则有内容。即使其他文件包含内容,此代码也会停止连接这些文件。
我用python编写了以下代码:
for items_1 in path:
if items_1.endswith('file1.blastn.xlsx'):
file1 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file1'])
elif items_1.endswith('file2.blastn.xlsx'):
file2 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file2'])
elif items_1.endswith('file3.blastn.xlsx'):
file3 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file3'])
elif items_1.endswith('file4.blastn.xlsx'):
file4 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file4'])
all_table = pd.concat([file1, file2,file3,file4], axis=1, join='outer', ignore_index=False)
all_table_dataframe = pd.DataFrame(all_table)
next_in.to_excel(str(header_file) + '.final.xlsx')所以我想要一个异常,它可以在空文件的情况下继续连接文件。somenone可以向我解释吗,一个继续对其余文件执行此操作的异常。谢谢
发布于 2020-02-25 02:12:30
也许只是一个尝试/除了?
for items_1 in path:
try:
if items_1.endswith('file1.blastn.xlsx'):
file1 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file1'])
elif items_1.endswith('file2.blastn.xlsx'):
file2 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file2'])
elif items_1.endswith('file3.blastn.xlsx'):
file3 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file3'])
elif items_1.endswith('file4.blastn.xlsx'):
file4 = pd.read_excel(items_1, sep='\t', names=['type', 'ID', 'type2', 'Name_file4'])
all_table = pd.concat([file1, file2,file3,file4], axis=1, join='outer', ignore_index=False)
all_table_dataframe = pd.DataFrame(all_table)
next_in.to_excel(str(header_file) + '.final.xlsx')
except:
continuehttps://stackoverflow.com/questions/60381536
复制相似问题