CSV (Comma-Separated Values) is one of the popular and accessible file formats for storing and exchanging tabular data.
CSV (Comma Separated Values) is a simple file format for storing tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line in the file is a data record. Each record has one or more fields, separated by commas. The comma used as the field separator is the source of the name of this file format. Python has a built-in module called CSV that works with CSV files.
Reading from a CSV file is done using the reader function (i.e. csv.readers()) which returns each row as a list. The CSV file is opened as a text file with the help of Python’s built-in open() function, which returns a file object.
In this example, we first open the CSV file in READ mode, the file object is converted to csv.reader object, and further operation takes place.
import csv
# Open the CSV file and read it
with open('data.csv', 'r') as file:
csv_reader = csv.reader(file)
# Iterate over each row in the CSV file
for row in csv_reader:
print(row) # Each row is a list of values
If the CSV file contains headers, you can use csv.DictReader(), which reads each row as a dictionary where keys are the column headers.
Suppose we have a details.csv file that contains the following headers name, age, and city.
name, age, city
John, 30, Delhi
Aman, 26, Pune
Bob, 35, Hyderabad
In this example, csv.DictReader() reads each row of the CSV file as a dictionary where the keys are the column headers, and the values are the corresponding values in each row.
import csv
# Reading a CSV file with headers
with open('data_with_headers.csv', 'r') as file:
csv_reader = csv.DictReader(file)
# Iterate over each row, now as a dictionary
for row in csv_reader:
print(row) # Each row is a dictionary with column names as keys
To write data to a CSV file first, we open the CSV file in the write mode. We then use csv.writer() or csv.DictWriter() function for writing dictionaries. The DictWriter() function is used to write CSV with headers.
Following is a code example of writing a CSV file.
import csv
# Data to write (list of lists)
data = [
["Name", "Age", "City"], ["John", 30, "Delhi"], ["Aman", 26, "Pune"], ["Bob", 35, "Hyderabad"]
]
# Write to a CSV file
with open('output.csv', 'w', newline='') as file:
csv_writer = csv.writer(file)
# Writing each row of data
csv_writer.writerows(data)
Let’s write a CSV file with headers using the csv.DictWriter() function:
import csv
# Data to write (list of lists)
data = [
["Name", "Age", "City"],["John", 30, "Delhi"], ["Aman", 26, "Pune"], ["Bob", 35, "Hyderabad"]
]
# Write to a CSV file with headers
with open('output_with_headers.csv', 'w', newline='') as file:
# Specify the fieldnames (column names)
fieldnames = ['Name', 'Age', 'City']
csv_writer = csv.DictWriter(file, fieldnames=fieldnames)
# Write the header (column names)
csv_writer.writeheader()
# Write the rows (dictionary values)
csv_writer.writerows(data)
Modifying CSV data in Python specifies that we can read the data, modify it in Python, and then write it back to the CSV file.
This example reads the CSV file, modifies Bob's age, and writes the updated data back to the file.
import csv
# Read data from the existing CSV file
with open('data.csv', 'r') as file:
csv_reader = csv.reader(file)
data = list(csv_reader)
# Modify the data (e.g., update age)
for row in data:
# If the name is 'Bob'
if row[0] == "Bob":
# Update age to 25
row[1] = 25
# Write the modified data back to the CSV file
with open('data.csv', 'w', newline='') as file:
csv_writer = csv.writer(file)
csv_writer.writerows(data)
In summary, The csv.reader() function reads a CSV file line by line, treating each line as a list of values, while csv.DictReader() reads each line as a dictionary, using column headers as keys On the other hand, csv .writer () allows writing to a CSV file where each line is written as a list, and csv.DictWriter() writes the data into a dictionary with the specified column title, ensuring that each field is formatted properly.
CSV (Comma-Separated Values) is one of the popular and accessible file formats for storing and exchanging tabular data.
CSV (Comma Separated Values) is a simple file format for storing tabular data, such as a spreadsheet or database. A CSV file stores tabular data (numbers and text) in plain text. Each line in the file is a data record. Each record has one or more fields, separated by commas. The comma used as the field separator is the source of the name of this file format. Python has a built-in module called CSV that works with CSV files.
Reading from a CSV file is done using the reader function (i.e. csv.readers()) which returns each row as a list. The CSV file is opened as a text file with the help of Python’s built-in open() function, which returns a file object.
In this example, we first open the CSV file in READ mode, the file object is converted to csv.reader object, and further operation takes place.
pythonimport csv # Open the CSV file and read it with open('data.csv', 'r') as file: csv_reader = csv.reader(file) # Iterate over each row in the CSV file for row in csv_reader: print(row) # Each row is a list of values
If the CSV file contains headers, you can use csv.DictReader(), which reads each row as a dictionary where keys are the column headers.
Suppose we have a details.csv file that contains the following headers name, age, and city.
pythonname, age, city John, 30, Delhi Aman, 26, Pune Bob, 35, Hyderabad
In this example, csv.DictReader() reads each row of the CSV file as a dictionary where the keys are the column headers, and the values are the corresponding values in each row.
pythonimport csv # Reading a CSV file with headers with open('data_with_headers.csv', 'r') as file: csv_reader = csv.DictReader(file) # Iterate over each row, now as a dictionary for row in csv_reader: print(row) # Each row is a dictionary with column names as keys
To write data to a CSV file first, we open the CSV file in the write mode. We then use csv.writer() or csv.DictWriter() function for writing dictionaries. The DictWriter() function is used to write CSV with headers.
Following is a code example of writing a CSV file.
pythonimport csv # Data to write (list of lists) data = [ ["Name", "Age", "City"], ["John", 30, "Delhi"], ["Aman", 26, "Pune"], ["Bob", 35, "Hyderabad"] ] # Write to a CSV file with open('output.csv', 'w', newline='') as file: csv_writer = csv.writer(file) # Writing each row of data csv_writer.writerows(data)
Let’s write a CSV file with headers using the csv.DictWriter() function:
pythonimport csv # Data to write (list of lists) data = [ ["Name", "Age", "City"],["John", 30, "Delhi"], ["Aman", 26, "Pune"], ["Bob", 35, "Hyderabad"] ] # Write to a CSV file with headers with open('output_with_headers.csv', 'w', newline='') as file: # Specify the fieldnames (column names) fieldnames = ['Name', 'Age', 'City'] csv_writer = csv.DictWriter(file, fieldnames=fieldnames) # Write the header (column names) csv_writer.writeheader() # Write the rows (dictionary values) csv_writer.writerows(data)
Modifying CSV data in Python specifies that we can read the data, modify it in Python, and then write it back to the CSV file.
This example reads the CSV file, modifies Bob's age, and writes the updated data back to the file.
pythonimport csv # Read data from the existing CSV file with open('data.csv', 'r') as file: csv_reader = csv.reader(file) data = list(csv_reader) # Modify the data (e.g., update age) for row in data: # If the name is 'Bob' if row[0] == "Bob": # Update age to 25 row[1] = 25 # Write the modified data back to the CSV file with open('data.csv', 'w', newline='') as file: csv_writer = csv.writer(file) csv_writer.writerows(data)
In summary, The csv.reader() function reads a CSV file line by line, treating each line as a list of values, while csv.DictReader() reads each line as a dictionary, using column headers as keys On the other hand, csv .writer () allows writing to a CSV file where each line is written as a list, and csv.DictWriter() writes the data into a dictionary with the specified column title, ensuring that each field is formatted properly.
Python is commonly used for developing websites and software, task automation, data analysis, and data visualisation. Since it's relatively easy to learn, Python has been adopted by many non-programmers, such as accountants and scientists, for a variety of everyday tasks, like organising finances.
Learning Curve: Python is generally considered easier to learn for beginners due to its simplicity, while Java is more complex but provides a deeper understanding of how programming works.
The point is that Java is more complicated to learn than Python. It doesn't matter the order. You will have to do some things in Java that you don't in Python. The general programming skills you learn from using either language will transfer to another.
Read on for tips on how to maximize your learning. In general, it takes around two to six months to learn the fundamentals of Python. But you can learn enough to write your first short program in a matter of minutes. Developing mastery of Python's vast array of libraries can take months or years.
6 Top Tips for Learning Python
The following is a step-by-step guide for beginners interested in learning Python using Windows.
Best YouTube Channels to Learn Python
Write your first Python programStart by writing a simple Python program, such as a classic "Hello, World!" script. This process will help you understand the syntax and structure of Python code.
The average salary for Python Developer is ₹5,55,000 per year in the India. The average additional cash compensation for a Python Developer is within a range from ₹3,000 - ₹1,20,000.
Copyrights © 2024 letsupdateskills All rights reserved