我在python中有一个函数,它基本上将三个txt文件合并成一个xlsx格式的文件。为此,我使用pandas包。因此,我在某个目录中运行python函数。此函数将输入作为特定路径。然后该函数采用此路径,列出该目录下的文件,并过滤所需的文件。这意味着,因为我只想读取txt文件,所以我会过滤txt文件。但是,当我尝试将此txt文件转换为pandas dataframe时,dataframe为None。另外,我希望将最终的xlsx写入初始文件所在的目录。下面是我的函数:
def concat_files(path):
summary=''
files_separate=[]
arr2 = os.listdir(mypath)
for i, items_list in enumerate(arr2):
if len(items_list) > 50:
files_separate.append(items_list)
files_separate
chunks= [files_separate[x:x+3] for x in range(0,len(files_separate),3)]
while chunks:
focus=chunks.pop(0)
for items_1 in focus:
if items_1.endswith('.Cox1.fastq.fasta.usearch_cluster_fast.fasta.reps.fasta.blastn.report.txt.all_together.txt'):
pandas_dataframe=pd.Dataframe(example)
pandas_dataframe.to_excel('destiny_path/' + str(header_file)+'.final.xlsx')发布于 2021-08-25 13:31:37
在导出xlsx文件之前,您需要创建文件夹。所以假设你已经创建了文件夹。
更改此行
pandas_dataframe.to_excel('destiny_path/' + str(header_file)+'.final.xlsx')至
pandas_dataframe.to_excel(os.path.join('destiny_path' ,str(header_file),'.final.xlsx'))https://stackoverflow.com/questions/68923773
复制相似问题