English 中文(简体)
在使用os.path.walk时,在转让前提及的各种问题
原标题:Problems with variable referenced before assignment when using os.path.walk

OK. I have some background in Matlab and I m now switching to Python. I have this bit of code under Pythnon 2.6.5 on 64-bit Linux which scrolls through directories, finds files named GeneralData.dat , retrieves some data from them and stitches them into a new data set:

import pylab as p
import os, re
import linecache as ln

def LoadGenomeMeanSize(arg, dirname, files):
        for file in files:
            filepath = os.path.join(dirname, file)
            if filepath == os.path.join(dirname, GeneralData.dat ):
                data = p.genfromtxt(filepath)
                if data[-1,4] != 0.0: # checking if data set is OK 
                    data_chopped = data[1000:-1,:] # removing some of data
                    Grand_mean = data_chopped[:,2].mean()
                    Grand_STD = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) + sum((data_chopped[:,2]-Grand_mean)**2))/sum(data_chopped[:,4]))
                else:
                    break
            if filepath == os.path.join(dirname, ModelParams.dat ):
                l = re.split(" ", ln.getline(filepath, 6))
                turb_param = float(l[2])                
                arg.append((Grand_mean, Grand_STD, turb_param))

GrandMeansData = []
os.path.walk(os.getcwd(), LoadGenomeMeanSize, GrandMeansData)
GrandMeansData = sorted(GrandMeansData, key=lambda data_sort: data_sort[2])

TheMeans = p.zeros((len(GrandMeansData), 3 ))
i = 0
for item in GrandMeansData:
    TheMeans[i,0] = item[0]
    TheMeans[i,1] = item[1]
    TheMeans[i,2] = item[2]
    i += 1

print TheMeans # just checking...
# later do some computation on TheMeans in NumPy

这使我感到迷惑(尽管我wear忙在一个月的工作):

Traceback (most recent call last):
  File "/home/User/01_PyScripts/TESTtest.py", line 29, in <module>
    os.path.walk(os.getcwd(), LoadGenomeMeanSize, GrandMeansData)
  File "/usr/lib/python2.6/posixpath.py", line 233, in walk
    walk(name, func, arg)
  File "/usr/lib/python2.6/posixpath.py", line 225, in walk
    func(arg, top, names)
  File "/home/User/01_PyScripts/TESTtest.py", line 26, in LoadGenomeMeanSize
    arg.append((Grand_mean, Grand_STD, turb_param))
UnboundLocalError: local variable  Grand_mean  referenced before assignment

一切权利......因此,我进行了一些阅读,并提出了这一全球变量:

import pylab as p
import os, re
import linecache as ln

Grand_mean = p.nan
Grand_STD = p.nan
def LoadGenomeMeanSize(arg, dirname, files):
        for file in files:
            global Grand_mean
            global Grand_STD
            filepath = os.path.join(dirname, file)
            if filepath == os.path.join(dirname, GeneralData.dat ):
                data = p.genfromtxt(filepath)
                if data[-1,4] != 0.0: # checking if data set is OK 
                    data_chopped = data[1000:-1,:]  # removing some of data
                    Grand_mean = data_chopped[:,2].mean()
                    Grand_STD = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) + sum((data_chopped[:,2]-Grand_mean)**2))/sum(data_chopped[:,4]))
                else:
                    break
            if filepath == os.path.join(dirname, ModelParams.dat ):
                l = re.split(" ", ln.getline(filepath, 6))
                turb_param = float(l[2])                
                arg.append((Grand_mean, Grand_STD, turb_param))

GrandMeansData = []
os.path.walk(os.getcwd(), LoadGenomeMeanSize, GrandMeansData)
GrandMeansData = sorted(GrandMeansData, key=lambda data_sort: data_sort[2])

TheMeans = p.zeros((len(GrandMeansData), 3 ))
i = 0
for item in GrandMeansData:
    TheMeans[i,0] = item[0]
    TheMeans[i,1] = item[1]
    TheMeans[i,2] = item[2]
    i += 1

print TheMeans # just checking...
# later do some computation on TheMeans in NumPy

它没有造成错误的批量。 甚至提供数据档案......但数据是错误的! 我通过指挥对其中一些人进行人工检查:

import pylab as p
data = p.genfromtxt(filepath)
data_chopped = data[1000:-1,:]
Grand_mean = data_chopped[:,2].mean()
Grand_STD = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) 
+ sum((data_chopped[:,2]-Grand_mean)**2))/sum(data_chopped[:,4]))

在选定的档案中公布。 它们是不同的:

(1) 谁能向我解释什么错误?

2) 是否有人知道解决办法?

我感谢:

Cheers, PTR

问题回答

I would say this condition is not passing: if filepath == os.path.join(dirname, GeneralData.dat ):

也就是说,你没有在模拟Params.dat之前获得Data.dat将军。 也许你们需要先发药,否则档案就不存在。

我看到法典和你提供的解决办法中的一个问题。

Never hide the issue of "variable referencing before assignment" by just making the variable visible. Try to understand why it happened?

在创建全球变量“Grand_mean”之前,你在获得任何价值之前,正在获得大象。 在这种情况下,将职能之外的各种变数初步化,将其定为全球,只会掩盖问题。

你看到了错误的结果,因为现在你把变数看上去了我的全球形象,但问题依然存在。 你们的大梅南从来没有像一些正确数据一样。

这意味着,法典中“如果档案馆=os.path.join(dirname.......)的一节从未执行过。

利用全球不是正确的解决办法。 如果你真的想提及和指定全球“大地”名称,这才有意义。 解释者在职能申报中为派任经营人辩护的方式造成了不统一的必要性。

我首先应在<代码>范围内将违约值分配给。 你拥有4个分支机构中的1个,实际上向大麦分配一个价值,这个数值在一个循环中具有正确的属性意义。 您有可能陷入这样的境地:

if filepath == os.path.join(dirname, ModelParams.dat ): is true, but either if filepath == os.path.join(dirname, GeneralData.dat ): or if data[-1,4] != 0.0: is not. It s likely the second condition that is failing for you. Move the

快速和 d脏的答案很可能是,你需要重新排列自己的法典。

...
            if filepath == os.path.join(dirname, GeneralData.dat ):
                data = p.genfromtxt(filepath)
                if data[-1,4] != 0.0: # checking if data set is OK 
                    data_chopped = data[1000:-1,:]  # removing some of data
                    Grand_mean = data_chopped[:,2].mean()
                    Grand_STD = p.sqrt((sum(data_chopped[:,4]*data_chopped[:,3]**2) + sum((data_chopped[:,2]-Grand_mean)**2))/sum(data_chopped[:,4]))

                    if filepath == os.path.join(dirname, ModelParams.dat ):
                        l = re.split(" ", ln.getline(filepath, 6))
                        turb_param = float(l[2])                
                        arg.append((Grand_mean, Grand_STD, turb_param))
                else:
                    break

...




相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...

热门标签