python delete specific line and re-assign the line number
Question: I would like delete specific line and re-assign the line number:

eg:

    
    
    0,abc,def
    1,ghi,jkl
    2,mno,pqr
    3,stu,vwx
    

what I want: if line 1 is the line need to be delete, then output should be:

    
    
    0,abc,def
    1,mno,pqr
    2,stu,vwx
    

What I have done so far:

    
    
    f=open(file,'r')
    lines = f.readlines()
    f.close()
    f.open(file,'w')
    for line in lines:
        if line.rsplit(',')[0] != 'line#':
            f.write(line)
    f.close()
    

above lines can delete specifc line#, but I don't konw how to rewrite the line
number before the first ','


Answer: I noticed that your account wasn't created in the past few hours, so I figure
that there's no harm in giving you the benefit of the doubt. You will really
have more fun on StackOverflow if you spend the time to learn its culture.

I wrote a solution that fits your question's criteria on a file that's already
written (you mentioned that you're opening a text file), so I assume it's a
CSV.

I figured that I'd answer your question differently than the other solutions
that implement the [CSV reader
library](https://docs.python.org/2/library/csv.html) and use a temporary file.

    
    
    import re
    numline_csv = re.compile("\d\,")
    
    # substitute your actual file opening here
    so_31195910 = """
    0,abc,def
    1,ghi,jkl
    2,mno,pqr
    3,stu,vwx
    """
    so = so_31195910.splitlines()
    
    # this could be an input or whatever you need 
    delete_line = 1
    line_bank = []
    
    for l in so:
        if l and not l.startswith(str(delete_line)+','):
            print(l)
            l = re.split(numline_csv, l)
            line_bank.append(l[1])
    
    so = []
    for i,l in enumerate(line_bank):
        so.append("%s,%s" % (i,l))
    

And the output:

    
    
    >>> so
    ['0,abc,def', '1,mno,pqr', '2,stu,vwx']
    

