Deleting lines in python
<p>I was just wondering how to delete lines in python.</p>

<p>Firstly I have opened up the csv file (using the with statements and whatnot), however the first couple of lines are unneccessary. They go along the lines of this:</p>

<p>**version 2.0 keyboard strokes</p>

<p>version 1.5 mouse wheel</p>

<p>...       ..**</p>

<p>[data starts here which i will manipulate]</p>

<p>Any functions I could use to perheps prompt the user with the number of rows theyd like to delete for future spreadsheets?</p>

<pre><code>    from __future__ import print_funciton
    import fileinput
    import csv

    with open('filename.csv', delimiter=",")

           readcsv = csv.reader(csvfile, delimiter=",")
           skip_rows = int(input('How many rows to skip? '))

           for i in range(skip_rows):
                 _ = next(readcsv)
           for row in readcsv:
                 print(row,end='')
</code></pre>

<p>Alternatively, I tried this, but it doesnt seem to get rid of the rows that have blank spaces (blank rows included in the number of rows I'd like to delete). Meanwhile, the code above appears to get rid of those blank spaces, but there is line termination.</p>

<pre><code>    next(filecsv) for i in range(10)
</code></pre>

<p>You can prompt the user with a simple while loop and listen into <a href="https://docs.python.org/3/library/sys.html#sys.stdin" rel="nofollow">standard input</a> or using the <a href="https://docs.python.org/3/library/functions.html#input" rel="nofollow">input()</a> function. </p>

<p>As to your question on how to delete lines in a file, you can read in the file as a list of lines.</p>

<pre><code>lines=[]
with open('input.txt') as f:
    lines=f.readlines()
</code></pre>

<p>You can then write back into the file everything past the lines you want to skip by using <a href="https://docs.python.org/2/tutorial/introduction.html#lists" rel="nofollow">list slicing</a>.</p>

<p>Also I am pretty sure similar questions have been asked before, try to Google or search Stack Overflow for your question or a subset of your question next time.</p>

<p><strong>P.S.</strong> 
I also want to add that if you are reading a very large file, it would be better if you read a line at a time, and outputted to a separate file. For a large enough file, you might run out of RAM to hold the file in memory.</p>

python csv