2

I am trying to copy rows from one CSV to another, and also to remove rows after being copied from the first file.

Please see the code, it is working for only copying specific rows from one CSV file to another, but it is not removing the rows from file 1.

import csv
import os

File1 = 'in.csv'
File2 = 'out.csv'

with open(File1, "r") as r, open(File2, "a") as w: 
     reader = csv.reader(r, lineterminator = "\n")
     writer = csv.writer(w, lineterminator = "\n")

     for counter,row in enumerate(reader):
         if counter<0: continue
         if counter>50:break
         writer.writerow(row)

with open(File1, "w") as r: 
     reader = csv.writer(r, lineterminator = "\n")

     for counter,row in enumerate(reader):
         if counter<0: continue
         if counter>50:break
         reader.writerow(row)

Please let me know how this would work after coping the rows to the File 2 and then remove those rows from File 1.

Here is the error:

for counter,row in enumerate(reader):
TypeError: '_csv.writer' object is not iterable
Martin Evans
  • 45,791
  • 17
  • 81
  • 97
Sarjan
  • 51
  • 1
  • 1
  • 3
  • 1
    Possible duplicate of [How to Delete Rows CSV in python](http://stackoverflow.com/questions/16271331/how-to-delete-rows-csv-in-python) – running.t Mar 15 '17 at 11:10
  • but this is not working can you solve this – Sarjan Mar 15 '17 at 11:13
  • please mention the error you getting while running this code? Also provide the sample data on which you working. Seems mostly data issue from the description – Priyank Mehta Mar 15 '17 at 11:18
  • @Sarjan: What is not working? Did you read the answers on above linked question? `You can't remove rows from a file but you can rewrite it with only the ones you want.` – Chankey Pathak Mar 15 '17 at 11:18
  • it's just coping rows from one csv file to another.. but I want to know how to remove the rows from file one.. Im just a beginner in python – Sarjan Mar 15 '17 at 11:20
  • the above answer is about to compare two csv not my solution s there – Sarjan Mar 15 '17 at 11:27
  • 2
    The solution *is* there. Read what @ChankeyPathak said; *You have to re-write the file without the rows that you want removed* – SiHa Mar 15 '17 at 12:41
  • @Sarjan, Have a look at the `pandas` module. this will make your life much much easier. If you have a `pandas` question, there are tons of articles, tutorials, etc. If you get stuck the Pandas community here on Stack is pretty friendly – MattR Mar 15 '17 at 14:03

1 Answers1

2

You appear to be trying to move the first 50 rows from one file and append them to another. This needs to be done in a few steps:

  1. Read each of the 50 rows from in.csv and append each one to out.csv.

  2. Write the remaining rows from in.csv to a temporary file.

  3. Delete the original in.csv file and rename the temporary file to be in.csv.

For example:

import itertools
import csv
import os

file_in = 'in.csv'
file_out = 'out.csv'
file_temp = '_temp.csv'

with open(file_in, "r", newline='') as f_input, \
     open(file_out, "a", newline='') as f_output, \
     open(file_temp, "w", newline='') as f_temp:
    
    csv_input = csv.reader(f_input)

    # Append first 50 rows to file_out
    csv.writer(f_output).writerows(itertools.islice(csv_input, 0, 50))

    # Write the remaining rows from file_in to file_temp
    csv.writer(f_temp).writerows(csv_input)

# Rename f_temp to file_in, first remove existing file_in then rename temp file
os.remove(file_in)
os.rename(file_temp, file_in)

This assumes you have a standard comma delimited file and that you are using Python 3.x.

Martin Evans
  • 45,791
  • 17
  • 81
  • 97
  • it's slicing every character and appending. Not copying from delimiter – Veronica Aug 25 '20 at 08:27
  • 1
    It assumes a standard CSV format with comma delimiters. The original script was also designed for Python 2.x, I have updated and tested it with Python 3.7. – Martin Evans Aug 25 '20 at 09:40