Delete used line in csv and move to an other csv file

42 Views Asked by At

I'm currently skipping used data lines but i actually want to move the used dataline to a new csv file so that i have a clear divider between used and unused data.

i'm currently using:

data = []
with open(path_to_csv_file) as f:
    reader = csv.reader(f, delimiter=',')
    for line in reader:
        data.append(line)

print(data)

what i'm looking for:

  1. Read data
  2. print data
  3. move data to new "used" csv file
  4. delete used data line
  5. repeat
1

There are 1 best solutions below

0
Zach Young On

You can effectively delete a row from a CSV by reading from the original and then just not writing to the new CSV. Then, replace the original file with the new.

If we start with this original.csv:

| Name  | Department   |
|-------|--------------|
| Alice | Finance      |
| Bobby | Marketing    |
| Chuck | Marketing    |
| Daryl | Mailing Svcs |

We can effectively move any row with Marketing to a new marketing CSV, and preserve other rows, by:

  1. opening both output-marketing.csv and output-new.csv for writing, and creating csv.writers for each
  2. reading from original
    1. copying the header (if you have one)
    2. for each row (of data):
      1. writing to output-marketing.csv if Marketing
      2. else, writing to output-new.csv:
import csv

f_mkt = open("output-marketing.csv", "w", newline="")
writer_mkt = csv.writer(f_mkt)

f_new = open("output-new.csv", "w", newline="")
writer_new = csv.writer(f_new)


f_in = open("original.csv", newline="")
reader = csv.reader(f_in)

header = next(reader)
writer_mkt.writerow(header)
writer_new.writerow(header)

for row in reader:
    if row[1] == "Marketing":
        writer_mkt.writerow(row)
    else:
        writer_new.writerow(row)

and that gives us output-marketing.csv:

| Name  | Department |
|-------|------------|
| Bobby | Marketing  |
| Chuck | Marketing  |

and output-new.csv:

| Name  | Department   |
|-------|--------------|
| Alice | Finance      |
| Daryl | Mailing Svcs |

If you have a script this simple, that exits as soon as it's done reading/writing, you don't need to bother with closing the files yourself, or with with open(...) as x statements: Python will close any open files as it exits your program/script.

Once your confident in the process you can add some code to move new to the original:

import shutil

shutil.move("output-new.csv", "original.csv")