0
def usunPsa(self, ImiePsa):

    with open('schronisko.csv', 'rb') as input, open('schronisko.csv', 'wb') as output:
        writer = csv.writer(output)
        for row in csv.reader(input):
                if row[0] == ImiePsa:
                    writer.writerow(row)
    with open(self.plik, 'r') as f:
            print(f.read())


Dsac;Chart;2;2020-11-04
Dsac;Chart;3;2020-11-04
Dsac;Chart;4;2020-11-04
Lala;Chart;4;2020-11-04
Sda;Chart;4;2020-11-04
Sda;X;4;2020-11-04
Sda;Y;4;2020-11-04
pawel;Y;4;2020-11-04`

If I use usunPsa("pawel") every line gets removed.

Following code earse my whole csv file instead only one line with given ImiePsa, What may be the problem there?

8
  • The input and output files are the same, and you're reading while also writing. That's a problem. Read the entire file first, then write the file. Or read it line by line while writing out to a new, temporary file and finally delete the CSV and rename the temporary file. Commented Nov 4, 2020 at 19:09
  • I am not sure what you want to do here. Do you want to write only the rows that equal whatever is stored in your ImiePsa variable? Commented Nov 4, 2020 at 19:10
  • probably no row in the csv matches ImiePsa. Are you certain there is a matching row? Commented Nov 4, 2020 at 19:10
  • @ÁdámMaul yes, exactly Commented Nov 4, 2020 at 19:11
  • 1
    Also, CSV files are text, not binary. Your file modes should not include "b". Commented Nov 4, 2020 at 19:15

2 Answers 2

1

I found the problem. row[0] in your code returns the entire row, that means the lines are not parsed correctly. After a bit of reading, I found that csv.reader has a parameter called delimiter to specify the delimiter between columns.

Adding that parameter solves your problem, but not all problems though.

The code that worked for me (just in case you still want to use your original code):

import csv

def usunPsa(ImiePsa):
    with open('asd.csv', 'rb') as input, open('schronisko.csv', 'wb') as output:
        writer = csv.writer(output)
        for row in csv.reader(input, delimiter=';'):
            if row[0] == ImiePsa:
                writer.writerow(row)

usunPsa("pawel")

Notice that I changed the output filename. If you want to keep the filename the same however, you have to use Hamza Malik's answer.

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

Comments

0

Just read the .csv file into memory as a list, then edit that list, and then write it back to the .csv file.

lines = list()

members= input("Please enter a member's name to be deleted.")

with open('mycsv.csv', 'r') as readFile:

    reader = csv.reader(readFile)

    for row in reader:

        lines.append(row)

        for field in row:

            if field == members:

                lines.remove(row)

with open('mycsv.csv', 'w') as writeFile:

    writer = csv.writer(writeFile)

    writer.writerows(lines)

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.