简体   繁体   English

导入存储在python模块中的csv数据

[英]Importing csv data stored in a python module

I have a scientific computing project for which I'm running an analysis and would like some data to be kept with the python module. 我有一个科学计算项目,我正在运行分析,并希望与python模块保存一些数据。 I'd like the data to be 'importable' for use within a couple of examples. 我希望数据是“可导入的”,可以在几个例子中使用。

The project folder hierarchy looks like this: 项目文件夹层次结构如下所示:

~/parent/
    setup.py
    setupegg.py
    /project
        __init__.py
        core.py
        /data
            __init__.py
            load_data.py
            somedata.csv
        /examples
            __init__.py
            ex1.py

I've run python setupegg.py develop from the project home folder so this package is importable from anywhere on my computer. 我从项目主文件夹运行python setupegg.py develop ,因此这个包可以从我的计算机上的任何地方导入。 This works so far. 这项工作到目前为止。

The file load_data.py looks like this: load_data.py文件如下所示:

import pandas as pd
df = pd.read_csv("somedata.csv")

And the file ex1.py looks like this: 文件ex1.py看起来像这样:

from test_module.data.load_data import df
def run():
    print df

But now when I run from project.examples import ex1 or from project.data import load_data I receive an IOError message saying 'somedata.csv' does not exist. 但是现在当我from project.examples import ex1from project.data import load_data运行时from project.examples import ex1我收到一条IOError消息,说'somedata.csv'不存在。

If I add this to the preamble of the data/load_data.py file 如果我将它添加到data / load_data.py文件的前导码中

import os
print os.listdir('./')

It prints the list of files/folder in the directory that I'm working from. 它打印我正在工作的目录中的文件/文件夹列表。

Of course, if I use absolute pathnames to the data then it will load perfectly. 当然,如果我对数据使用绝对路径名,那么它将完全加载。 How can I adjust the import statements so that reading of the csv files in the data folder does not use absolute pathnames? 如何调整import语句,以便读取数据文件夹中的csv文件不使用绝对路径名?

If the csv is in the same directory as the module that opens it then you could try using the __file__ attribute: 如果csv与打开它的模块位于同一目录中,那么您可以尝试使用__file__属性:

import os
import pandas as pd
df = pd.read_csv(os.path.join(os.path.dirname(__file__), "somedata.csv"))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM