gpt-3.5-turbo-0613· OpenAI
WildChat-4.8M (AI2)
write me some python 3 code that appends all rows with a variable string in them to…
- User
write me some python 3 code that appends all rows with a variable string in them to another csv file.
- Assistant
Sure, here's an example of Python 3 code that reads a CSV file and appends all rows containing a specific variable string to another CSV file:
pythonimport csv def append_rows_with_string(filename, string, output_file): with open(filename, 'r') as file: reader = csv.reader(file) rows_to_append = [row for row in reader if string in row] if rows_to_append: with open(output_file, 'a', newline='') as file: writer = csv.writer(file) writer.writerows(rows_to_append) # Example Usage input_filename = 'input.csv' output_filename = 'output.csv' string_to_find = 'variable string' append_rows_with_string(input_filename, string_to_find, output_filename)Make sure to replace with the actual filename of your input CSV file, with the desired filename for your output CSV file, and with the variable string you want to search for in the rows.