-1

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

khelwood
  • 55,782
  • 14
  • 81
  • 108
tardos93
  • 235
  • 2
  • 17
  • This looks like a way overcomplicated approach to solving something very simple. What does the `air.txt` contain and what's your end goal? – zwer Jul 21 '17 at 12:30
  • here you can see, what cointains the air.txt https://stackoverflow.com/questions/45231230/separate-the-txt-file-contents-to-multiple-cells-in-csv-file/45231512#45231512 – tardos93 Jul 21 '17 at 12:34

2 Answers2

1

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)
cs95
  • 379,657
  • 97
  • 704
  • 746
1

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')
Fabio Lamanna
  • 20,504
  • 24
  • 90
  • 122
  • I get a very long error message and i can opening the csv file only "reading" mode – tardos93 Jul 21 '17 at 12:49
  • that sounds weird, you can check options to read csv files with pandas [here](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) – Fabio Lamanna Jul 21 '17 at 13:20