首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >继续连接文件的代码中的异常

继续连接文件的代码中的异常
EN

Stack Overflow用户
提问于 2020-02-25 01:59:18
回答 1查看 34关注 0票数 0

所以我将3个xlsx文件连接成一个。但有时,其中一个文件可能是空的,而另一个文件则有内容。即使其他文件包含内容,此代码也会停止连接这些文件。

我用python编写了以下代码:

代码语言:javascript
复制
  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可以向我解释吗,一个继续对其余文件执行此操作的异常。谢谢

EN

回答 1

Stack Overflow用户

发布于 2020-02-25 02:12:30

也许只是一个尝试/除了?

代码语言:javascript
复制
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:
        continue
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/60381536

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档