Reading CSV files in Python
There are several ways to read a CSV file in Python, but the most common method is to use the csv module.Using CSV module
Python csv module implements classes to read and write tabular data in CSV format. Here's an example of how to use the csv module to read a CSV file:
import csv
with open('file.csv', 'r') as file:
reader = csv.reader(file)
for row in reader:
print(row)
In the above example, the open() function is used to open the file in read mode ('r'). The csv.reader() function is then used to create a CSV reader object, which is passed the file object. The for...loop iterates through each row in the CSV file and prints it. The with keyword allows us to both open and close the file without having to explicitly close it. csv.reader(file)
The csv.reader function takes one argument, which is the file to be read. It returns an iterator (an object that can be iterated over) that yields rows of the CSV file as lists of strings. Each row represents a record in the CSV file and each element of the list corresponds to a field (column) in the record. You can also use next() function to get the header of the csv if the first row contain the header.
import csv
with open("data.csv", "r") as file:
csv_reader = csv.reader(file)
headers = next(csv_reader)
print(headers)
for row in csv_reader:
print(row)
This will print first row as headers and rest of the rows as normal data.
csv.DictReader() class
The csv.DictReader class is used to read a CSV file and convert each row of the file into a dictionary. The keys of the dictionary correspond to the fields (columns) in the CSV file and the values correspond to the values in the respective fields.
The following example shows how to use the csv.DictReader class to read a CSV file called "data.csv" and print the contents of the file as a list of dictionaries:
import csv
with open("data.csv", "r") as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
print(row)
This will read the file "data.csv" and print each row of the file as a dictionary. where the keys are the headers of the csv file and values are the corresponding values.
Dictionary key value
You can also use the keys of the dictionary to access the values of a specific field in a row:
import csv
with open("data.csv", "r") as file:
csv_reader = csv.DictReader(file)
for row in csv_reader:
print(row["field_name"])
This will print the values of the field "field_name" in each row.
The csv.DictReader class is useful when you want to work with the data in a more structured way, for example if you want to access the values of a specific field in a row by its name.
It assumes that the first row of the CSV file contains the field names and uses them as the keys for the dictionaries. If the first row of the file does not contain the field names, you can pass a list of fieldnames to the fieldnames parameter of the DictReader constructor to specify them manually.
Using pandas library
Alternatively, you can use the pandas library to read the csv file, which is a powerful library for data manipulation and analysis. Here's an example of how to use pandas to read a CSV file:
import pandas as pd
df = pd.read_csv('file.csv')
print(df)
This will return a DataFrame, which is a 2-dimensional labelled data structure with columns of potentially different types. You can also use various options to control how the CSV file is read such as skip rows, set column names, etc.
You can also use other parameters of read_csv function to customize the loading of the csv like,
df = pd.read_csv("data.csv", delimiter='\t', header=None, names=["col1","col2","col3"])
This will read the file "data.csv" with tab separator, no header and assign the column names as "col1","col2","col3".
Python Pandas
Pandas is a powerful and flexible open-source data manipulation and analysis library in Python. It is a great tool for data wrangling and preparation, as well as data analysis and modeling.CSV Files
A CSV (Comma Separated Values) file is a plain text file that stores tabular data in a simple format, where each line of the file represents a row of the table and each field (column) within that row is separated by a comma. CSV files are often used for data export and import, and can be opened and edited in a variety of spreadsheet and text editing software.
Related Topics
- Print the following pattern using python
- Python Program to Check Leap Year
- Remove first n characters from a string | Python
- Check if the first and last number of a list is the same | Python
- Number of occurrences of a substring in a string in Python
- Remove last element from list in Python
- How to Use Modulo Operator in Python
- Enumerate() in Python
- Writing to a File using Python's print() Function
- Dictionary Comprehension in Python
- How to Convert List to String in Python
- How to convert int to string in Python
- Random Float numbers in Python