0

I have the following loop, where cfile has been defined as 'C_' previously.

for i in range(15,25):
    infile = cfile + str(i+1) + '.txt'
    C + str(i+1) = np.loadtxt(infile, delimiter=',')

I have many files in the working directory named C_1, C_2, etc.

I'd like to import these files into Python and name them as C_1, C_2, etc, but I'd only like to do so for that certain range. I am not understanding why the str(i+1) will not work.

wjandrea
  • 28,235
  • 9
  • 60
  • 81
  • Which version of Python are you using? – 101 Jan 19 '21 at 01:10
  • 1
    Does this answer your question? [How do I create variable variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-variable-variables) and [SyntaxError: cannot assign to operator](https://stackoverflow.com/q/8956825/4518341) – wjandrea Jan 19 '21 at 01:12
  • BTW, "not working" is generally not helpful. For debugging, we need to know *exactly* what error you're getting, preferably with the full traceback. See [mre] for reference, and [ask] if you want other tips. – wjandrea Jan 19 '21 at 01:14
  • 2
    I would recommend you use dictionaries or append these files to a list named `C` then each variable will be `C[0]` instead of `C_0` and you'll be able to do some logic based on the numbers too – Boris Verkhovskiy Jan 19 '21 at 01:16

2 Answers2

1

You can use glob to open all files with a specified pattern. Although it's patterns follow Unix guidelines and may need to be further filtered to meet your specifications:

import glob

files = glob.glob("C_[1-2][1-9].txt")

Or just load all the correct files into a dict as it's the recommended way of creating a variable amount of variables.

files = {f"C{i}": np.loadtxt(f"{cfile}{i}.txt", delimiter=',') for i in range(16,26)}
Jab
  • 26,853
  • 21
  • 75
  • 114
0
cfile='C_'
C='What_'
H={}
for i in range(15,25):
    infile = cfile + str(i+1) + '.txt'
    #print("infile:",infile)
    k=C+str(i+1)
    H[k] = np.loadtxt(infile, delimiter=',')
    print(f"Your loaded file is here: H[{k}]")

    #PRINT OUT THE CONTENT OF THE FILE 
    print(H[k])
    
 

just use print(H[k]) to print out the content of each file. Or print( H["What_24"] ), or if you change the string of variable C = 'C_' then you can print(H["C_24"] ) and view the content of file stored inside the dict.

Why what you wanted to do was an error:

C + str(i+1) = np.loadtxt(infile, delimiter=',')

Because C is undefined ... and if it were defined like C='What_you_want' you would have gotten:

'What_you_want'+str(i+1)  ->   'What_you_want'+'number' -> 'What_you_wantnumber' 

So

'What_you_wantnumber' is a string which resides inside the variable C

np.loadtxt(infile, delimiter=',') is a method that return the content o the file as a string .

so you can't assign string = string . But you can do

variable = string

for example: the variable name = "flagello" ok

 print(name)  

output : flagello #that is as string

"flagello"="flagello"
  File "<stdin>", line 1
SyntaxError: can't assign to literal
"flagello"="silicio"
  File "<stdin>", line 1
SyntaxError: can't assign to literal
'flagello' == 'silico'  

Output: False

'flagello' == 'flagello'

Output: True

You can compare strings  but you can t assing a string to a string .

You can assign a string value  to a variable. 

But at this moment nothing rename nothing . Now you get the content of the files , assign the content of each file inside a dict ad print the content of the dict

How to rename a file

import os
os.rename(r'C_21.txt',r'CCCP_21.txt')

In this case you don't need to read the content of the file to rename it.

Just use the old name and the new name

Mario Abbruscato
  • 819
  • 1
  • 7
  • 9
  • This worked, but I am not understanding why I cannot use print(C_24) to pull up the file. I continue to get the NameError: name is not defined, which I thought the loop should have taken care of. – silicoflagellate Jan 19 '21 at 21:54
  • See edited answer: just use print(H[k]) to print out the content of each file. Or print(H["What_24"] ), or if you change the string of variable C = 'C_' then you can print(H["C_24"] ) – Mario Abbruscato Jan 19 '21 at 22:17
  • I hope I have clarified your doubts – Mario Abbruscato Jan 19 '21 at 23:07