0

I'm trying to add the functionality of writing information from one CSV to a new CSV file if certain inputs are met. I'm struggling to get the functions that are executed to also write into the new CSV file. This is my current (very basic) function to print the information that I want to add out;

def function():
    with open('my_file.csv', 'r') as csv.file:

        csv_reader = csv.reader(csv.file)

        next(csv_reader)

        for line in csv_reader:
            print(line)

Thanks in advance - my python is quite primitive!

Subhanshuja
  • 390
  • 1
  • 3
  • 20
JackA
  • 1
  • 1
    Your code shows no conditions. Doesn't the code you posted do what you expect? (Though obviously fix the broken indentation.) – tripleee Dec 31 '19 at 12:54
  • 1
    This should help https://stackoverflow.com/questions/3146571/read-and-write-on-same-csv-file ... Your question is similar – Jordan Simba Dec 31 '19 at 12:55
  • Your question is not unique, i think another answer from another question will give you suggestion : https://stackoverflow.com/a/18851971/1384360 – Subhanshuja Dec 31 '19 at 12:57
  • Thanks for all your help! – JackA Dec 31 '19 at 15:33

1 Answers1

0

This might be helpful with your case

fileInput = open(fileInput.csv, 'rb')
fileOutput = open(fileOutput.csv, 'wb')
reader = csv.reader(fileInput)
writer = csv.writer(fileOutput)
for row in reader:
   if row[2] == 'Condition':
      writer.writerow( row[0], row[1], 'Somevalue')