1

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()
1
  • I suggest simply load the data you have in the csv file that you want to make change, then store them into a list. Make the necessary changes and write them into a csv file. Since the functions that you wrote repeat the for loop 3 times and repeat the write 2 times when you are calling those functions, so it would cause troubles. Commented Dec 12, 2018 at 5:26

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.