2

I need to remove the second line of my csv file.

I am using the code below but unfortunately it doesn't work.

data = ""
adresse = "SLV.csv"

if os.path.exists(adresse) :
    f = open(adresse,"ab")
    writer = csv.writer(f,delimiter = ",") 
    reader = csv.reader(open(adresse,"rb") , delimiter = ",")


    for line in reader:

        if reader.line_num == 2:    
            writer.writerow(line) 

    f.close()
1
  • 2
    What happens when you run this? Commented Dec 11, 2014 at 16:16

5 Answers 5

2

Since all you want to do is remove the second line, using the csv module is overkill. It doesn't matter if the file is comma separated data or Vogon poetry. Write the front parts, skip the middle part and write the end.

import shutil

# generate test file
with open('x.txt', 'w') as f:
    for i in range(10):
        f.write('line %d\n' % i)

# skip one line
with open('x.txt','rb') as rd, open('x.txt', 'rb+') as wr:
    wr.write(rd.readline())
    rd.readline()
    shutil.copyfileobj(rd, wr)
    wr.truncate()

print open('x.txt').read()
Sign up to request clarification or add additional context in comments.

Comments

1

write to a temp file and update the original after:

if os.path.exists(adresse) :
    with open(adresse,"r") as f,open("temp.csv" "a+") as temp:    
        writer = csv.writer(temp,delimiter = ",")
        reader = csv.reader(f , delimiter = ",")
        for ind, line in enumerate(reader):
            if ind == 2:
                continue
            else:
               temp.writerow(line)
        temp.seek(0)
        with open(adresse,"w") as out:
            reader = csv.reader(temp , delimiter = ",")
            writer = csv.writer(out,delimiter = ",")
            for row in reader:
                writer.writerow(line)

If the files can be read into memory just call list on reader and remove the second element:

if os.path.exists(adresse) :
    with open(adresse,"r") as f:
        reader = list(csv.reader(f , delimiter = ","))
        reader.pop(1)
        with open(adresse,"w") as out:
            writer = csv.writer(out,delimiter = ",")
            for row in reader:
                writer.writerow(row)

1 Comment

I would like to avoid to write a temp file. Is that doable ? Cheers
0

if i understand you correctly you are trying to make a new file and you don't want to insert the line number 2. if this is your scenario there is a trivial bug in your procedure, that is:

if reader.line_num != 2:    
        writer.writerow(line) 

2 Comments

I don t want to make a new file. I just want to correct this one. Do I necessarily have to write an entire new file ? I have to that on 16 000 files so I am defo looking for something more efficient. cheers pal
@Dirty_Fox - Riccardo is just pointing out your error - your code writes line 2 only, his fix is to skip line 2.
0

Here my solution, less code for you to write:

>>> import pyexcel as pe         #  pip install pyexcel
>>> sheet = pe.load("SLV.csv")
>>> del sheet.row[1]             # first row starts at index 0
>>> sheet.save_as("SLV.csv")

Comments

0

I do agree with tdelaney, and this is a far more compact solution

lines = open('x.txt', 'r').readlines()
lines.pop(1)
open('x.txt', 'w').writelines(lines)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.