gpt4 book ai didi

python - 如何在python中批量读取pandas模块的read_csv()

转载 作者:太空宇宙 更新时间:2023-11-03 15:52:44 24 4
gpt4 key购买 nike

我想使用 python pandas 模块合并许多 .txt 文件。代码如下:

import os
import pandas as pd
root='D:\daymet'
newfile=pd.DataFrame() #all txt data will append on this variable
for file in os.listdir(root):
if 'txt' in file:
pathname=os.path.join(root,file) #file path
temp=pd.read_csv(pathname) # read csv
newfile.append(temp,ignore_index=True) #append
newfile.to_csv('D:\\merge.csv') #save as a newfile

报告错误如下:

EmptyDataError                            Traceback (most recent call last)
<ipython-input-9-4282b0cc482a> in <module>()
6 if 'txt' in file:
7 pathname=os.path.join(root,file)
----> 8 temp=pd.read_csv(pathname)
9 newfile.append(temp,ignore_index=True)
10 newfile.to_csv('D:\\merge.csv')

......

pandas\parser.pyx in pandas.parser.TextReader.__cinit__ (pandas\parser.c:6162)()

EmptyDataError: No columns to parse from file

最佳答案

您错过了第 8 行和第 9 行中的选项卡(您获得了没有“txt”的文件,并尝试在不定义路径的情况下打开它)。

import os
import pandas as pd
root='D:\daymet'
newfile=pd.DataFrame() #all txt data will append on this variable
for file in os.listdir(root):
if 'txt' in file:
pathname=os.path.join(root,file) #file path <--add additional tab here
temp=pd.read_csv(pathname) # read csv <--add additional tab here
newfile.append(temp,ignore_index=True) #append
newfile.to_csv('D:\\merge.csv') #save as a newfile

关于python - 如何在python中批量读取pandas模块的read_csv(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41120034/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com