0

I wrote the following code to take the fourth column of an xlsx file and save it in a csv file, if that matters. But it gives the following error: ValueError: write to closed file.

from __future__ import division, print_function import sys import numpy as np import csv import os import xlrd import csv import unicodecsv def xls_to_csv1(xls_filename, csv_filename): wb = xlrd.open_workbook(xls_filename) sh = wb.sheet_by_index(0) with open(csv_filename,"wb") as fh: csv_out = unicodecsv.writer(fh, encoding='utf-8', delimiter=';') #print(sh.row_values(1)[3]) for row_number in range (1, sh.nrows): row = [] count=0 for col in sh.row_values(row_number): if count==3: row.append(col) count=count+1 csv_out.writerow(row) xls_filename='Attachment_1_Test_Data_1_Seed.xlsx' csv_filename='Summary_csv.csv' xls_to_csv1(xls_filename,csv_filename) 
5
  • 2
    The code should not run in any version of python since Book objects don't have a .split method. Change wb = xlrd.open_workbook(xls_filename).split() to wb = xlrd.open_workbook(xls_filename). Commented Apr 13, 2017 at 13:20
  • But it was working on Python 2.7 though. Anyway,when I remove the split; it gives a surprising error: ValueError: write to closed file. But how is it closed? I opened it in "wb" mode. Commented Apr 13, 2017 at 13:25
  • (1) I am very confident the code you have shown in your question did not work in Python 2.7. If you did have code that worked in Python 2.7, perhaps you have not transcribed it properly into this question. (2) What were you trying to do with split anyway? You say it worked in Python 2.7. What did it do when it was working? Commented Apr 13, 2017 at 13:49
  • @JohnY My goal, like I said in the question, was to isolate the fourth column, so that I could save only that in another file. To do that, I use split; otherwise, columns weren't isolatable (I mean, statements like print(sh.row_values(1)[3]) were not working, so I figured that must be the problem, so I used split) Commented Apr 13, 2017 at 13:53
  • @JohnY Anyway, you can ignore the deal about split. As it turns out from this gentleman Abdou's answer, the main issue was something entirely different. So, I think I will edit the question and remove the .split() from the original code. Then it'd be a more meaningful question for other patrons. Commented Apr 13, 2017 at 13:55

1 Answer 1

2

The process of appending data to the csv file should be done inside the with_statement. This is because a with_statement is supported through what is known as context manager. Basically, it means that if you open a file with a with_statement, then exiting the with_statement closes the file. That's why you're seeing the closed file error. The following should do:

from __future__ import division, print_function import sys import numpy as np import csv import os import xlrd import csv import unicodecsv def xls_to_csv1(xls_filename, csv_filename): wb = xlrd.open_workbook(xls_filename) sh = wb.sheet_by_index(0) with open(csv_filename,"wb") as fh: csv_out = unicodecsv.writer(fh, encoding='utf-8', delimiter=';') #print(sh.row_values(1)[3]) for row_number in range (1, sh.nrows): row = [] count=0 for col in sh.row_values(row_number): if count==3: row.append(col) count=count+1 csv_out.writerow(row) 

I hope this helps.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.