I’'m looking to do some File and Data type validation in Python. I’m overwhelmed by the number of options to do this. What is your favourite way (code, use a specific package/library) to check if:
- What the imported file type in Python is (e.g. check if its CSV)
- If the column in the CSV file is char, int etc?
If you are looking to extract .csv
files only you can use glob
import glob
# Filter for CSV file in "my_folder"
csv_files = glob.glob('.../my_folder/*.csv')
I suggest using the pandas library if you will be manipulating CSV or spreadsheet data. It will make your life much easier and it is widely used.
import pandas as pd
# Load CSV file into dataframe
df = pd.read_csv('my_csv.csv')
# Get column datatype (Int, String, Float, etc)
df.dtypes
Hope this will help you to know more about…importing csv file