How to create, read and write csv file from python

First Lets talk about csv

Its a plain text format which comes from (Comma Separated Values)

Most spreadsheet application supports this. You can open and edit it from excl, Libra office cal , google sheets and other spread sheet application, also supported by most of the programming language.

For simplicity and flexibility often used in data science.

Create Our first csv file

We will create our csv file with as simple way as possible. Even we will not use any spreadsheet application.

Open any text editor. You can open vs code or geany or your beloved code editor.

In the text editor. create and save a new file named colors.cvs Make sure you properly added the .csv extinction.

Now add this text in the file.

Color,Red,Green,Blue,Hex Code
Red,255,0,0,#FF0000
Green,0,255,0,#00FF00
Blue,0,0,255,#0000FF
Yellow,255,255,0,#FFFF00
Orange,255,165,0,#FFA500
Purple,128,0,128,#800080
Cyan,0,255,255,#00FFFF
Magenta,255,0,255,#FF00FF
Pink,255,192,203,#FFC0CB
Lime,0,255,0,#00FF00
Teal,0,128,128,#008080
Brown,165,42,42,#A52A2A
Maroon,128,0,0,#800000
Navy,0,0,128,#000080
Olive,128,128,0,#808000
Sky Blue,135,206,235,#87CEEB
Gold,255,215,0,#FFD700
Silver,192,192,192,#C0C0C0

save it . That’s all.

Now we will read the csv file from python.

Before that we need to be clear about python file mode

Mode Description
‘r’ Read-only mode. (Read): Open a file to only read its contents.
‘w’ Write-only mode. (Write): Create a new file or completely rewrite an existing one. (erases old data!)
‘a’ (Append): The data is written to the end of the file.
‘r+,’ ‘w+,’ ‘a+’ Read/write mode. These allow both reading and writing, with variations on how existing data is handled

Python have build in csv module.

To read csv file we will use

csv.reader


csv.DictReader

csv.reader

  • Turns each row of your CSV file into a list of the individual values.
  • Example: [‘Sky Blue’, ‘135’, ‘206’, ‘235’, ‘#87CEEB’]

csv.DictReader

  • Turns each row of your CSV into a dictionary (like a little address book).
  • Uses the first row of the CSV as keys (headers) for the dictionaries.
  • Example: {‘Color’: ‘Sky Blue’, ‘Red’: ‘135’, ‘Green’: ‘206’, ‘Blue’: ‘235’, ‘Hex Code’: ‘#87CEEB’}

Now open our colors.csv with python

Example with csv.reader:

The csv.reader function turns each row of the CSV file into a list of individual values. The example



import csv

with open('colors.csv', 'r', newline='') as csvfile:
	reader = csv.reader(csvfile)
	next(reader)
	for row in reader:
		print(row)
		color_name = row[0]
		red_value = int(row[1])
		green_value = int(row[2])
		blue_value = int(row[3])
		hex_code = row[4]

		# We can now process the color data as needed
		print(f"Color: {color_name} (R: {red_value}, G: {green_value}, B: {blue_value}), Hex Code: {hex_code}")





csv.DictReader

The csv.DictReader function turns each row of the CSV file into a dictionary, using the first row as keys (headers). Example:



import csv

with open('colors.csv', 'r', newline='') as csvfile:
	reader = csv.DictReader(csvfile)


	for row in reader:
		print(row)
		color_name = row['Color']
		red_value = int(row['Red'])
		green_value = int(row['Green'])
		blue_value = int(row['Blue'])
		hex_code = row['Hex Code'] 

		print(f"Color: {color_name} (R: {red_value}, G: {green_value}, B: {blue_value}), Hex Code: {hex_code}")




csv writer

The csv.writer function allows you to write data from lists to a CSV file. Example:



import csv

colors_to_write = [
    ['White', 255, 255, 255, '#FFFFFF'],
    ['Black', 0, 0, 0, '#000000'],
    ['Gold', 255, 215, 0, '#FFD700']
]

with open('new_colors.csv', 'w', newline='') as csvfile:
    writer = csv.writer(csvfile)
    writer.writerow(['Color', '255', '0', '0', '#FF0000'])  # Write the header row
    writer.writerows(colors_to_write)  # Write the data rows






CSV DictWriter

The csv.DictWriter function allows you to write data from dictionaries to a CSV file. Example:



import csv

colors_to_write = [
    {'Color': 'White', '255': 255, '0': 255, '#FF0000': '#FFFFFF'},
    {'Color': 'Black', '255': 0, '0': 0, '#FF0000': '#000000'},
    {'Color': 'Gold', '255': 255, '0': 215, '#FF0000': '#FFD700'},
] 

with open('new_colors_dict.csv', 'w', newline='') as csvfile:
    fieldnames = ['Color', '255', '0', '#FF0000'] 
    writer = csv.DictWriter(csvfile, fieldnames=fieldnames)
    writer.writeheader()  # Write the header row
    writer.writerows(colors_to_write)  # Write the data rows





Now a practical approach . Analysis our csv data



import csv
import matplotlib.pyplot as plt


filename = 'colors.csv'

red_values = []
green_values = []
blue_values = []

with open(filename, 'r', newline='') as csvfile:
	reader = csv.reader(csvfile)
	next(reader)
	for row in reader:
		red_values.append(int(row[1]))
		green_values.append(int(row[2]))
		blue_values.append(int(row[3]))


# Visualize histograms of red, green, and blue values
fig, axs = plt.subplots(3)
fig.suptitle('Histograms of RGB Values')
axs[0].hist(red_values, bins=20, color='red')
axs[0].set_xlabel('Red Value')
axs[0].set_ylabel('Frequency')
axs[1].hist(green_values, bins=20, color='green')
axs[1].set_xlabel('Green Value')
axs[1].set_ylabel('Frequency')
axs[2].hist(blue_values, bins=20, color='blue')
axs[2].set_xlabel('Blue Value')
axs[2].set_ylabel('Frequency')
plt.show()




Related Posts

Create And Train Simple Artificial Neuron with python : Teach a Neuron to Sum two number
July 1, 2024

We will start this example with simplest neuron then train it to sum two number so it will be simple. And best part is for simplicity we will use only python standard library. We will write everything from scratch.. First the basics. Like our biological neuron artificial neuron can learn and change over time. Human […]

Converting Between Python Dictionaries and JSON
May 30, 2024

We will use json.dums() function to convert to json from python dictionary and json.loads() to convert from json to python dictionary. In this example code we used flower details data in python dictionary and converted it to json. import json # flowers in python dictionary flowers = { “rose”: { “scientific_name”: “Rosa”, “grows_in”: [“gardens”, “wild”], […]

Display Matplotlib Plots in Pygame: A Step-by-Step Guide
March 16, 2024

Our tasks for this Import necessary module import sys import pygame import matplotlib from pygame.locals import * import matplotlib.pyplot as plt from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas This code imports a tool from Matplotlib that lets you save plots as image files (like PNG or JPG). Use this for […]