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