I have several .txt file with similar name, such as data_00, data_01, ..., data_99. I tried to use np.loadtxt in a for loop to read it.
Now, I can use r-string to read the values in the text file manually
numerical = np.loadtxt(r'file_path\data_0.txt', delimiter=' ')
, it is working but I am thinking to loop through the files.
So when I tried to use f-string, but it is giving me error "SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape"
number = np.arange(100)
for i in number:
numerical = np.loadtxt(f'file_path\data_{i}.txt', delimiter=' ')
I also tried to use glob to import the value, (code shown below), not it is not giving me the correct values in same order.
file_path = 'file_path'
file_pattern = "*.txt"
files_list = glob(os.path.join(file_path,file_pattern))
for f in files_list:
print(f'----- Loading {f} -----')
numerical = np.loadtxt(f, delimiter=' ', unpack=True)