1

I have a lot of txt, I want to read it, and plus them(every txt have same shape of array)

    for i in base_n:
        dfp_base=np.loadtxt(base_n,skiprows=2,usecols=(1,2,3))
        dfp_base+=dfp_base
    print dfp_base

but it's will only plus the end of files

I try to assign a variable but it's will give me an error

    for i in base_n:
        dfp_base=np.loadtxt(base_n,skiprows=2,usecols=(1,2,3))
        dfp_base_s+=dfp_base
    print dfp_base_s

UnboundLocalError: local variable 'dfp_base_s' referenced before assignment

how to fix it?

EDIT i define a zero array and solve this problem

dfp_base_s=np.zeros(shape=(30,3))
2
  • 1
    Did you forget to initialize dfp_base_s ? Commented May 7, 2020 at 5:37
  • the anwser is right! I need to define dfp_base_sat first Commented May 7, 2020 at 6:30

1 Answer 1

2

your problem that you are trying to assign to var that not referenced before assignment see on below:

for i in range(1,10): 
     dfp_base=1 
     dfp_base_s+=dfp_base 



NameError                                 Traceback (most recent call last)
<ipython-input-2-24596062a447> in <module>
      1 for i in range(1,10):
      2      dfp_base=1
----> 3      dfp_base_s+=dfp_base
      4 

NameError: name 'dfp_base_s' is not defined

but if you initialize before the loop that will work

 dfp_base_s = 0              

 for i in range(1,10): 
      dfp_base=1 
      dfp_base_s+=dfp_base 


 dfp_base_s                  
 9


Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.