I want to delete the last 16 and the first 16 lines in a csv file using Pyton 2.7.
what am i wrong in my code? Why not delete these rows?
import urllib2
import unicodecsv as csv
import os
import sys
import io
import time
import datetime
import pandas as pd
from bs4 import BeautifulSoup
import sys
import re
def to_2d(l,n):
return [l[i:i+n] for i in range(0, len(l), n)]
f = open('air.txt', 'r')
x = f.readlines()
filename=r'output.csv'
resultcsv=open(filename,"wb")
output=csv.writer(resultcsv, delimiter=';',quotechar = '"', quoting=csv.QUOTE_NONNUMERIC, encoding='latin-1')
maindatatable = to_2d(x, 4)
print maindatatable
output.writerows(maindatatable)
with open('output.csv', 'rb') as csvfile:
lines = csvfile.readlines()
lines = lines[16:-16]
cWriter = csv.writer(csvfile, delimiter=',')
for line in lines:
cWriter.writerow(line)
resultcsv.close()
Sorry if this seems like a really simple question, but I'm new to python and any help in this area would be greatly appreciated, thanks!!