0

Hello I'm a beginner in Python and I would like to know how to delete csv data using user input without having to use another file as a output. My code is:

import csv
mark = input("What would you like to delete")
with open('first.csv', 'r') as csv_file:
    csv_reader = csv.reader(csv_file)
    for line in csv_reader:
        if str(line[0]) != str(mark):
            pen = open('first.csv', 'a')
            pen.write(line[0] + '\n')
            print('sucessfully deleted')

Currently all the file does is print out a million sucessfully deleted but I'm confused on why it does this.

2
  • My advice is to spend some time learning some Python basics. You use open inside your for loop, do you really want to open a new file a million times? Commented May 11, 2020 at 18:18
  • You take the lines you want to delete and append them to the end of the file... so that they'll be read again and appended to the end of the file again. Did you have to cancel this? I think it will stop naturally when you run out of disk space. Commented May 11, 2020 at 18:21

2 Answers 2

3

If you don't want to create another file, then you need to read all the csv file content in memory, erasing the lines you don't want, and then save it back on the same file name (overwriting the old one). Something like this (untested):

import csv
mark = input("What would you like to delete")
with open('first.csv', 'r') as csv_file:
    csv_lines = list(csv.reader(csv_file))
kept_lines = []
for line in csv_lines :
    if str(line[0]) != str(mark):
        kept_lines.append(line)
    else:
        print('sucessfully deleted:', line[0])
with open('first.csv', 'w') as csv_file:
    csv.writer(csv_file).writerows(kept_lines)

Sign up to request clarification or add additional context in comments.

Comments

0

You could do something like this :

import pandas as pd
df = pd.read_csv("first.csv")
mark = input("What would you like to delete")
df[df.iloc[:, 0]!=mark].to_csv("first.csv")

1 Comment

Running through the rows individually is slow and dropping rows in the thing you are iterating will skip rows completely. I think this would work better as df[df.iloc[:, 0]!=mark].to_csv("first.csv").

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.