'Reading a sequence of txt files [closed]

I created a set, which has some specific names. But, these names are text files that I have to read, using the package Pandas.

For example, I can say that my set is like this:

{0: 'BDS00001', 1: 'BDS00002', 2: 'BDS00003', 3: 'BDS00013', 4: 'BDS00014', 5: 'BDS00015' ... 487: 'BDS01952', 488: 'BDS01953'}

There is any way that I can use a loop, read the dictionary, transform the BDS00001 into BDS00001.txt, and then read the BDS00001.txt using Pandas?



Solution 1:[1]

Try this.

all_files =  { 0: 'BDS00001', 1: 'BDS00002'}

for k,v in all_files.items():
    all_files[k]=v+'.txt'

print(all_files) # {0: 'BDS00001.txt', 1: 'BDS00002.txt'}


#________________________READING FILE________________________#

for a in all_files:
    with open(a) as data:
        print(data.read())

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1