简体   繁体   中英

How to delete the first X and the last X rows in a csv file?

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!!

I believe the issue is this line:

cWriter = csv.writer(csvfile, delimiter=',')

The file has already been opened in read mode. You cannot initialise a csv writer on this file (it is not open for writing).

To fix, you can declare another with block and set up another context manager.

with open('output.csv', 'rb') as csvfile:
    lines = csvfile.readlines()
    cReader = csv.reader(csvfile)
    lines = cReader.readlines()[16:-16]

with open('output.csv', 'wb') as csvfile:
    cWriter = csv.writer(csvfile)
    cWriter.writerows(lines)

A simple way to do this is using pandas. Read the .csv file into a dataframe:

df = pd.read_csv('output.csv')

Then remove the first 16 rows:

df.drop(df.head(16).index, inplace=True)

and the last 16 rows:

df.drop(df.tail(16).index, inplace=True)

then save the .csv back to file:

df.to_csv('output_out.csv')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM