How to read csv file in python flask?

by laury_ullrich , in category: Python , 2 years ago

How to read csv file in python flask?

Facebook Twitter LinkedIn Telegram Whatsapp

2 answers

by lea_kunde , a year ago

@laury_ullrich To read a CSV file in a Flask app, you can use the csv module that is part of the Python standard library. Here's an example of how you can use the csv module to read a CSV file and return the data as a list of dictionaries, where each dictionary represents a row in the CSV file:

1
2
3
4
5
6
7
8
9
import csv

def read_csv(file_path):
    data = []
    with open(file_path, 'r') as csv_file:
        reader = csv.DictReader(csv_file)
        for row in reader:
            data.append(row)
    return data


You can then use this function in your Flask app to read the CSV file and access the data. For example:

1
2
3
4
5
6
7
8
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    data = read_csv('data.csv')
    return str(data)


This will return the contents of the data.csv file as a string representation of a list of dictionaries. Each dictionary in the list will contain the data for a single row in the CSV file, with the keys being the column names and the values being the cell values for that row.


You can also use other libraries, such as pandas, to read CSV files in a Flask app. pandas is a popular data analysis library that provides a convenient way to read and manipulate data in a variety of formats, including CSV.


Here's an example of how you can use pandas to read a CSV file and return the data as a pandas DataFrame:

1
2
3
4
5
import pandas as pd

def read_csv(file_path):
    df = pd.read_csv(file_path)
    return df


You can then use this function in your Flask app to read the CSV file and access the data as a pandas DataFrame. For example:

1
2
3
4
5
6
7
8
from flask import Flask

app = Flask(__name__)

@app.route('/')
def index():
    df = read_csv('data.csv')
    return str(df)


This will return the contents of the data.csv file as a string representation of a pandas DataFrame. The DataFrame will contain the data from the CSV file, with the columns being the column names and the rows being the data for each row in the CSV file.

by cassandra , a year ago

@laury_ullrich 

There are different ways to read a CSV file in Python Flask. Here are two examples:

  1. Using the csv module:
1
2
3
4
5
6
7
8
9
import csv

with open('data.csv', 'r') as file:
    reader = csv.reader(file)
    data = []
    for row in reader:
        data.append(row)

# Now, data is a list of lists with the CSV data


  1. Using the pandas library:
1
2
3
4
import pandas as pd

data = pd.read_csv('data.csv')
# Now, data is a pandas DataFrame object with the CSV data


You can choose the option that better suits your needs depending on the size of your CSV file and the manipulations you want to perform with the data. Remember to adjust the filename parameter in open() or pd.read_csv() to match the actual name and path of your CSV file.