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

2024-09-30 20:32:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个函数“read\u 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\u csv”_nDI.py公司". 在同一目录中,我尝试以下脚本:

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

你在哪儿测试.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\u t”、“ZEZ2A\u t”等。另一方面,如果我简单地添加一行

read_csv('test.csv')

在同一个Python脚本中定义函数之后,它就可以工作了,并且在运行脚本之后我确实看到了这些变量。为什么只有在函数定义在同一个脚本中时它才起作用?你知道吗


Tags: 文件ofcsvtheto函数脚本read
1条回答
网友
1楼 · 发布于 2024-09-30 20:32:54

globals在您调用它的文件中提供全局变量,因此它总是提供定义read_csv的文件的全局变量。你知道吗

把全局名称空间弄得乱七八糟可能不是最好的主意。最好只返回整个数组。另外,我建议您看看^{}包,它可以很容易地将CSV读入DataFrame对象,该对象的工作方式类似于numpy数组,但对于大多数用途来说更为方便。你知道吗

相关问题 更多 >