pandas 阅读大量文档时出现“OSError:从文件初始化失败”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43454367/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 03:24:37  来源:igfitidea点击:

"OSError: Initializing from file failed" when reading lots of docs

pythonpython-3.xpandasglob

提问by TPike

I am reading in several years of data files from a directory then reducing the data to what I need and then putting it together in a single data frame, but I am getting an "OSError: Initializing from file failed", the key lines of my code are:

我正在从一个目录中读取几年的数据文件,然后将数据减少到我需要的数量,然后将它们放在一个数据框中,但是我收到了“OSError:从文件初始化失败”,我的关键行代码是:

data_list = []
count = 0
for file in glob.glob("mydir"):
    # read in file and name 
    name = "events" + str(count)
    name = pd.read_csv(file, sep = '\t' )

    #code to reduce file

    count += 1
    data_list.append(name)

all_events = pd.concat(data_list)

回答by Grr

Usually when I use glob I have to do so like this:

通常当我使用 glob 时,我必须这样做:

for file in glob.glob('mydir/*'):
    # do something

Otherwise I don't get the filenames that are in mydir. That may be part of your problem. Otherwise what seems odd is that you are initializing namewith name = "events" + str(count)then immediately overwriting it with a DataFrame. Not sure if that is the desired behavior as the initial name =would be frivolous.

否则我不会得到mydir. 这可能是您问题的一部分。否则,似乎什么奇怪的是,要初始化namename = "events" + str(count)随后立即用数据帧覆盖它。不确定这是否是所需的行为,因为最初的行为name =很无聊。