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)
Bookobjects don't have a.splitmethod. Changewb = xlrd.open_workbook(xls_filename).split()towb = xlrd.open_workbook(xls_filename).ValueError: write to closed file. But how is it closed? I opened it in "wb" mode.splitanyway? You say it worked in Python 2.7. What did it do when it was working?print(sh.row_values(1)[3])were not working, so I figured that must be the problem, so I used split).split()from the original code. Then it'd be a more meaningful question for other patrons.