4 Replies - 214 Views - Last Post: 07 August 2014 - 05:57 PM Rate Topic: -----

#1 helpme18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 21-October 09

Python-invalid mode (w+b) or filename

Posted 07 August 2014 - 09:31 AM

So this code is working with Excel sheets and it is supposed to grab the information from each column of readme.xlsx, stick in into Column B of the template and then save that. It suppose to do this for every column. So by the end of it there will be 80 new files if there are 80 columns in readme.xlsx. It seems to be parsing just fine but it keeps giving me the error, "invalid mode (w+B) or filename", but it saves my file anyway as instance0(which i want it to do), and the instance0 file is exactly the way i want it, but then it crashes the program and it won't create the files for the rest of the columns. However when i run the script again instance2 is created, and when i run it again instance3 is created. I don't want to have to keep doing this so how can I have this script run without crashing.
Thank You.

import xlwt
import xlrd
import xlutils
import datetime
from xlutils.copy import copy
from os.path import join


#Open the WorkBooks
workbook = xlrd.open_workbook('readme.xlsx')
sheet = workbook.sheet_by_index(0)

workbook2 = xlrd.open_workbook('template.xlsx')     #new
sheet2 = workbook.sheet_by_index(0)
#--------------

nrows=sheet.nrows
ncols=sheet.ncols

print "There are %d columns and %d rows"%(ncols,nrows)

wb=copy(workbook2)

#make this into a function that each filename can use. 
for nextcolumn in range(ncols):
    print nextcolumn
    columnB=[]
    count=0
    for i in range(nrows):
        columnB.append([sheet.cell_value(i, nextcolumn) ])      #row,column
    for s in columnB:
        #sheet.write(count,1,str(s))                            #row, column, value
        s=str(s)        
        wb.get_sheet(0).write(count,1,str(s));                  #row, column, value
        wb.save("instance"+str(nextcolumn)+'.xls')
        count+=1



Is This A Good Question/Topic? 0
  • +

Replies To: Python-invalid mode (w+b) or filename

#2 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,024
  • Joined: 12-December 12

Re: Python-invalid mode (w+b) or filename

Posted 07 August 2014 - 09:47 AM

I cannot be certain that this is the main issue, but:
    for s in columnB:

        wb.save("instance"+str(nextcolumn)+'.xls')

you are over-writing the same file (.xls) a number of times in a loop. You only need to save this file once, outside this loop.

for s in columnB:

    s = str(s)

You also shouldn't change the value of s (the loop-control/sentinel) within the loop.
    somethingElse = str(s)

This post has been edited by andrewsw: 07 August 2014 - 09:49 AM

Was This Post Helpful? 0
  • +
  • -

#3 helpme18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 21-October 09

Re: Python-invalid mode (w+b) or filename

Posted 07 August 2014 - 10:16 AM

View Postandrewsw, on 07 August 2014 - 10:47 AM, said:

I cannot be certain that this is the main issue, but:
    for s in columnB:

        wb.save("instance"+str(nextcolumn)+'.xls')

you are over-writing the same file (.xls) a number of times in a loop. You only need to save this file once, outside this loop.


No, i'm trying to create several files, one for each loop. As the loop goes through the column it adds to the 'nextcolumn' variable. So the files in the directory for 3 columns in readme would be instance0.xlsx,instance1.xlsx,instance2.xlsx

This post has been edited by andrewsw: 07 August 2014 - 10:25 AM
Reason for edit:: Fixed quote tags

Was This Post Helpful? 0
  • +
  • -

#4 andrewsw  Icon User is online

  • It's just been revoked!
  • member icon

Reputation: 3729
  • View blog
  • Posts: 13,024
  • Joined: 12-December 12

Re: Python-invalid mode (w+b) or filename

Posted 07 August 2014 - 10:27 AM

Yes, but the value of 'nextcolumn' doesn't change within this inner loop - and it shouldn't either as it is controlling the main (outer) loop.

This post has been edited by andrewsw: 07 August 2014 - 10:28 AM

Was This Post Helpful? 0
  • +
  • -

#5 helpme18  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 21-October 09

Re: Python-invalid mode (w+b) or filename

Posted 07 August 2014 - 05:57 PM

View Postandrewsw, on 07 August 2014 - 11:27 AM, said:

Yes, but the value of 'nextcolumn' doesn't change within this inner loop - and it shouldn't either as it is controlling the main (outer) loop.


Oh wow thanks for the tip, it worked. I got the older code to work by catching the exception and proceeding anyway:
try:
	wb.save("instance"+str(nextcolumn)+'.xls')
except Exception:
	pass



Which is just dumb. Thanks again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1