繁体   English   中英

在Python中,使用globals()导入函数

[英]In Python, import a function using globals()

我有函数“ read_csv”,该函数经常在其他Python脚本中使用。 与其将其复制到我在其中使用的每个Python脚本的开头,不如将其保存在单独的文件中并导入。 该函数定义如下:

import numpy as np

def read_csv(filename,suffix=''):
    # Read column headers (to be variable naames)
    with open(filename) as f:
        firstline = f.readline()                    # Read first line of csv
        firstline = firstline.replace("\n","")      # Remove new line characters
        firstline = firstline.replace(" ","")       # Remove spaces
        ColumnHeaders = firstline.split(",")        # Get array of column headers

    # Read in the data (omitting the first row containing column headers)
    data=np.genfromtxt(filename,skiprows=1,delimiter=",",filling_values=0)

    # Assign the data to arrays, with names of the variables generated from column headers
    Ind=0
    for Var in ColumnHeaders:
        VarName=Var+suffix                      # Define variable name appended with given suffix (if any)
        globals()[VarName]=data[:,Ind]          # Assign the columns of the data to variables names after the column headers

该函数读取一个csv文件,其中包含列标题和其下的数字,并将这些数字作为数组写入具有从列标题派生的名称的“工作区”中。 我将上面的代码保存为“ read_csv_nDI.py”。 在同一目录中,我尝试以下脚本:

import numpy as np
from read_csv_nDI import read_csv
read_csv('test.csv')

其中“ test.csv”是应正常工作的CSV文件:

Track, Bin, RO_T,ZEZ2A_T,Intra,RO_T_evnt_amp,ZEZ2A_T_evnt_amp,Intra_evnt_amp,Intra_reservoir_amplitude_normalized_to_RO_T,Intra_reservoir_amplitude_normalized_to_ZEZ2A_T
              1,              1,     2149.7307,     2110.3000,     2189.5908,     1000.3883,     -766.3962,     -687.7489,       -0.6875,        0.8974
              1,              2,     2151.7307,     2112.3000,     2191.5908,     1000.3883,     -766.3962,     -687.7489,       -0.6875,        0.8974
              1,              3,     2153.7307,     2114.3000,     2193.5908,     1000.3883,     -766.3962,     -687.7489,       -0.6875,        0.8974
              1,              4,     2155.7307,     2116.3000,     2195.5908,     1000.3883,     -766.3962,     -687.7489,       -0.6875,        0.8974
              1,              5,     2157.7307,     2118.3000,     2197.5908,     1000.3883,     -766.3962,     -687.7489,       -0.6875,        0.8974
              1,              6,     2159.7307,     2120.3000,     2199.5908,     1000.3883,     -766.3962,     -687.7489,       -0.6875,        0.8974
              1,              7,     2161.7307,     2122.3000,     2201.5908,     1000.3883,     -766.3962,     -687.7489,       -0.6875,        0.8974

但是,如果运行上述脚本并键入dir()命令,则不会看到我期望看到的变量“ RO_T”,“ ZEZ2A_T”等。 另一方面,如果我只是添加一行

read_csv('test.csv')

在同一个Python脚本中的函数定义之后,它可以工作,运行脚本后,我确实看到了这些变量。 为什么仅在函数定义在同一脚本中时才起作用?

globals为您提供了在调用它的文件中的全局变量,因此它始终为您提供了定义了read_csv的文件的全局变量。

这样杂乱全局名称空间可能不是最好的主意。 最好只返回整个数组。 另外,我建议您查看pandas程序包,该程序包可以轻松地将CSV读取到DataFrame对象中,该对象的作用类似于numpy数组,但对于大多数用途来说更方便。

暂无
暂无

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

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