Home > Mobile >  Find name of the uploaded CSV in Python / Pandas
Find name of the uploaded CSV in Python / Pandas

Time:12-06

I'm trying to fetch the name of the file I upload. I'm wrote a program which does a statistical test based on the data in the file, the program is currently set up in two steps:

1 - upload the file using the following methods:

from google.colab import files
import io
uploaded = files.upload()

This triggers a small "uploader" as a widget

I then upload the CSV file and my next set of code only needs to read the file name, here's the code

2 - read the data by specifying uploaded file name (let's say "filename" for ex.)

data = pd.read_csv(io.BytesIO(uploaded["filename.csv"]))

Every time I run this code, I need to manually update the name of the file, I'm trying to automate the part of fetching the filename so it can be read automatically.

Thanks

To upload the file:

from google.colab import files
import numpy as np
import pandas as pd
import io
uploaded = files.upload()

To read the file: (currently name of the file needs to be updated manually each time)

data = pd.read_csv(io.BytesIO(uploaded["filename.csv"]))

CodePudding user response:

The following contains the name of your csv

list(uploaded.keys())[0]

so your line should look like

data = pd.read_csv(io.BytesIO(uploaded[list(uploaded.keys())[0]]))
  • Related