More efficient way of reading a CSV file in Python with different delimeter

I am a high school student, so please don’t mind if this question is stupid, but Is there a more efficient way of reading a csv file in python than using the csv module and file reader. Also, is there a way to specify a different delimiter or can it only read with "," as a delimiter.

import csv

with open(filename, 'r') as csvfile:
    csvreader = csv.reader(csvfile)
    fields = next(csvreader)
    for row in csvreader:
        rows.append(row)

Thank you so much!

>Solution :

You may use the "pandas" library to read a csv file (along with sql tables etc) and store the data as a pandas "dataframe", you may use these dataframes directly in plotly and prophet. You can also convert these dataframes into numpy arrays or lists based on the necessity.

You can install pandas using the following command:

pip install pandas

or you may install it in a conda environment using:

conda activate name_of_your_environment
conda install pandas

After that you can read a csv file using the following code to read a csv file and specify any delimeters:

import pandas as pd

df = pd.read_csv("Path to File/filename.csv",delimiter = "\t")

To change it into an numpy.ndarray you may use the "to_numpy()" method of the dataframe.

Similarly, to change it into a list you may use the "tolist()" method of the dataframe.

I hope this helps.

Leave a Reply