monto
monto

Reputation: 61

Using python to make changes to a CSV file

Using I a python script I am trying to make changes to a CSV file by changing strings to the integers via a key, ie Male = 0 and Female =1. However, when I write to the CSV with the first function and do it again with the second function, it will overwrite the changes I have already made. Solutions? Or is there a better way to read and write to a CSV file?

import csv 

def category():
    for line in csv_reader:
        if line[1] == ("Male"):
                line[1] = "0"
        elif line[1] == ("Female"):
                line[1] = "1"
        csv_writer.writerow(line)

def pets():
    for line in csv_reader:
            if line[2] == ("Cat"):
                line[2] = "0"
            elif line[2] == ("Dog"):
                line[2] = "1"
            else: line[2] = "2"
            csv_writer.writerow(line)

with open('information.csv', 'r', encoding = 'utf') as csv_file:
   csv_reader = csv.reader(csv_file)
   next(csv_reader)

with open('updated_version.csv', 'w', encoding ='utf-8', newline ='') as 
new_file:
        csv_writer = csv.writer(new_file)
        for line in csv_reader:
            category()
            pets()

Upvotes: 1

Views: 961

Answers (2)

Shariq
Shariq

Reputation: 506

Simply open the file in append mode:

open('updated_version.csv', 'a', encoding ='utf-8', newline ='') 

from the docs:

``a''   Open for writing.  The file is created if it does not exist.  The
         stream is positioned at the end of the file.  Subsequent writes
         to the file will always end up at the then current end of file,
         irrespective of any intervening fseek(3) or similar.

 ``a+''  Open for reading and writing.  The file is created if it does not
         exist.  The stream is positioned at the end of the file.  Subse-
         quent writes to the file will always end up at the then current
         end of file, irrespective of any intervening fseek(3) or similar.

Upvotes: 0

votelessbubble
votelessbubble

Reputation: 805

You can use Pandas. It is a powerful library and you can do this in just a few lines

import pandas as pd

csv_file = pd.read_csv("information.csv")

# This opens the CSV file and now you can make changes here itself
# I will assume that your column 1 is called 'Gender' and column 2 is called 'Pets'

csv_file.loc[csv_file['Gender'] == 'Male','Gender'] = 0
csv_file.loc[csv_file['Gender'] == 'Female','Gender'] = 1

# Change for pets too

csv_file['Temporary_Pets_Column'] = 2
csv_file.loc[csv_file['Pets'] == 'Cat','Temporary_Pets_Column'] = 0
csv_file.loc[csv_file['Pets'] == 'Dog','Temporary_Pets_Column'] = 1

# Overwrite the pet's column with the temporary column.

csv_file['Pets'] = csv_file.pop('Temporary_Pets_Column')

# Save your csv file

csv_file.to_csv("updated_information.csv",index=False)

Upvotes: 3

Related Questions