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:
- Read data
- print data
- move data to new "used" csv file
- delete used data line
- repeat
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:
We can effectively move any row with Marketing to a new marketing CSV, and preserve other rows, by:
and that gives us output-marketing.csv:
and output-new.csv:
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 xstatements: 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: