Extract file name from read_csv - Python
I'd start with using pathlib
.
from pathlib import Path
And then leverage the stem
attribute and glob
method.
Let's make an import function.
def read_csv(f):
return pd.read_csv(table_list, sep="|")
The most generic approach would be to store in a dictionary.
p = Path('\test\test\csvfiles')
dod = {f.stem: read_csv(f) for f in p.glob('*.csv')}
And you can also use pd.concat
to turn that into a dataframe.
df = pd.concat(dod)
You can try something like this:
import glob
data = {}
for filename in glob.glob('/path/to/csvfiles/*.csv'):
data[filename[:-4]] = pd.read_csv(filename, sep="|", names=col)
Then data.keys()
is the list of filenames without the ".csv" part and data.values()
is a list with one pandas dataframe for each file.
Many ways to do it
for filename in os.listdir(path):
if filename.endswith('.csv'):
table_list.append(pd.read_csv(filename,sep="|"))
new_table_list.append(filename.split(".")[0])
One more
for filename in os.listdir(path):
if filename.endswith('.csv'):
table_list.append(pd.read_csv(filename,sep="|"))
new_table_list.append(filename[:-4])
and many more
As @barmar pointed out, better to append path as well to the table_list
to avoid any issues related to path and location of files and script.