How to find string in CSV and delete it?

There is a CSV file with roughly this structure:
000000000,Ivan,test
000000000,Ivan,test test test
000000000,Ivan,column
000000000,Ivan,bla bla bla
000000000,Ivan,oops
000000000,Ivan,take it
000000000,Ivan,start

I need to find a string value, for example "oops," then copy the value of the string variable and the string from the CSV to remove. How can I do that?
March 20th 20 at 11:21
3 answers
March 20th 20 at 11:23
Solution
from typing import List, Tuple
import csv

SOURCE_CSV = 'source.csv'
SUBSTR = 'ooops'
COL_WITH_VALUES = 1


def main():
 data = get_data_from_file(SOURCE_CSV)
 filtered_data, clone_rows = data_filter(data, SUBSTR, COL_WITH_VALUES)
 save_data(SOURCE_CSV, filtered_data)


def save_data(file_path: str, rows: List[List[str]]):
 with open(file_path, 'w', newline=") as f:
 w = csv.writer(f)
w.writerows(rows)


def data_filter(rows: List[List[str]], subst: str, col: int) -> Tuple[List[List[str]], List[List[str]]]:
 filtered_data = []
 clone_rows = []
 for row in data:
 if row[col] == subst:
rows_with_subst.append(row)
else:
filtered_data.append(row)
 return filtered_data, clone_rows


def get_data_from_file(file_path: str) -> List[List[str]]:
 with open(SOURCE_CSV, 'r') as f:
 data = [x for x in csv.reader(f)]
 return data


if __name__ == "__main__":
 main()
And what happens when not enough memory?

Why so much code? - Lee.Wi commented on March 20th 20 at 11:26
@kayla.Me
And what happens when not enough memory?

Then the Asker would come again with a question, what do I do with the file, which weighs 100500гб or my file is processed for the second day.
Why so much code?

How much should it be? - Mikayla24 commented on March 20th 20 at 11:29
@ottilie.Heathcote,

Well, then why Pro writes code that does not pass the volume test? )

Less code === fewer bugs === better - Lee.Wi commented on March 20th 20 at 11:32
Less code === fewer bugs === better

Thank you, neighing.
Let's all and all to write without functions, and the PLO will forget as a bad dream. And all the variable names I suggest sokratit to one, well the 2, characters. And even less to write, let's use everywhere global and goto. - Mikayla24 commented on March 20th 20 at 11:35
@ottilie.Heathcote, the problem is solved a few of the expressions of the generators with the code size and memory usage. - skye.Raynor commented on March 20th 20 at 11:38
March 20th 20 at 11:25
Read csv line by line and copy all the second file. Do check with your condition.
Then the original file is removed,and the new one renamed to the old
Why create a new file, then delete the old and then rename the new? This is how many unnecessary moves? You can just overwrite the old file with 'w'. - Mikayla24 commented on March 20th 20 at 11:28
@ottilie.Heathcote, if in the middle of the process fails, the data will be corrupted. - skye.Raynor commented on March 20th 20 at 11:31
@Trycia86, I just about Dmitry and write. - skye.Raynor commented on March 20th 20 at 11:34
March 20th 20 at 11:27
Easier, faster and better to load into pandas, sdela need a sample and upload the csv
faster and more correct, but not simpler - Velva commented on March 20th 20 at 11:30
Often the use of pandas is redundant. Why drag all these megabytes of pandas and its dependencies to handle a file weighing a few hundred kilobytes? - Mikayla24 commented on March 20th 20 at 11:33
From a gun on sparrows. Then wonder what sites weigh megabytes, programs - hundreds of megabytes, operating systems, hundreds of gigabytes... - Erling.Berni commented on March 20th 20 at 11:36

Find more questions by tags PythonCSV