Help with ZIP command in Python

classic Classic list List threaded Threaded
10 messages Options
Reply | Threaded
Open this post in threaded view
|

Help with ZIP command in Python

Ruben Geert van den Berg
Dear all,
 
I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like
 
value labels 1"bli"2"bla"....
 
I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:
 
begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?
 
TIA!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.
Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Jon K Peck

Here is a form that works.

begin program.
vallabs=['bli','bla','blo','blu']
lablist=" ".join([str(j)+" '" + k +"'" for j,k in zip(range(1, len(vallabs)+1),vallabs)])
print lablist
end program.

You need to generate a list with single string entries, so the j,k construct in the original does not work.
Also, while range generates a list that is appropriate for zipping, you need the list elements to be incremented inside the list construct.  Starting the range expression at 1 simplifies this.

But there is a specialized construct in Python for this sort of situation.  The enumerate function returns a sequence of pairs of sequential integers and values.  so this program is a little simpler if integer values is really what you need.

begin program.
vallabs=['bli','bla','blo','blu']
lablist = lablist=" ".join([str(j+1)+" '" + k +"'" for j,k in enumerate(vallabs)])
print lablist
end program.

Now to take this one step further, the following program uses an api from the spssaux.VariableDict class to generate and apply the value labels to the variable x.

data list free /x.
begin data
1
2
3
4
end data.

begin program.
import spss, spssaux
vallabs=['bli','bla','blo','blu']
vls = dict([(k+1,v) for k, v in enumerate(vallabs)])
print vls
vardict = spssaux.VariableDict()
vardict['x'].ValueLabels = vls
end program.

Final remark: if the value label text could contain a quote character, the value literals need to be "smart quoted".  There is a function called _smartquote in the spssaux module that will do this.

Regards,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/12/2010 07:38 AM
Subject: [SPSSX-L] Help with ZIP command in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like

value labels 1"bli"2"bla"....

I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:

begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.

Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Albert-Jan Roskam
In reply to this post by Ruben Geert van den Berg
Hi Ruben,
 
>>> [str(value + 1) + " '" + label + "'" for value, label in enumerate(vallabs)]
["1 'bli'", "2 'bla'", "3 'blo'", "4 'blu'"]
>>> [str(value + 1) + " '" + label + "'" for value, label in zip(range(len(vallabs)), vallabs)]
["1 'bli'", "2 'bla'", "3 'blo'", "4 'blu'"]
>>>
You could also consider using a dictionary.

Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/12/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: [SPSSX-L] Help with ZIP command in Python
To: [hidden email]
Date: Monday, July 12, 2010, 3:34 PM

Dear all,
 
I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like
 
value labels 1"bli"2"bla"....
 
I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:
 
begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?
 
TIA!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.

Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

John F Hall
In reply to this post by Ruben Geert van den Berg
What's wrong with opening an syntax file and writing:
 
val lab
   <v1> '<label1>'
   <v12> '<label2>'
 
etc ???
 
You have to write the labels out anyway and this keeps the syntax for future reference and amending if necessary.
----- Original Message -----
Sent: Monday, July 12, 2010 3:34 PM
Subject: Help with ZIP command in Python

Dear all,
 
I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like
 
value labels 1"bli"2"bla"....
 
I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:
 
begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?
 
TIA!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.
Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Albert-Jan Roskam
In reply to this post by Jon K Peck
Hi Jon,
 
The last example you give is interesting. So the VariableLabel attribute is set by assigning to a new dictionary, right? Nice syntax to have vardict['x'].ValueLabels before the assignment operator.
But why can't the following be used to rename variables?
 
begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
newlist = ["prefix_" + vardict[str(v)].VariableName for v in vardict]
#setattr(vardict, "VariableName", newlist)
vardict.VariableName = newlist
print vardict.VariableName
end program.


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/12/10, Jon K Peck <[hidden email]> wrote:

From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] Help with ZIP command in Python
To: [hidden email]
Date: Monday, July 12, 2010, 4:20 PM


Here is a form that works.

begin program.
vallabs=['bli','bla','blo','blu']
lablist=" ".join([str(j)+" '" + k +"'" for j,k in zip(range(1, len(vallabs)+1),vallabs)])
print lablist
end program.

You need to generate a list with single string entries, so the j,k construct in the original does not work.
Also, while range generates a list that is appropriate for zipping, you need the list elements to be incremented inside the list construct.  Starting the range expression at 1 simplifies this.

But there is a specialized construct in Python for this sort of situation.  The enumerate function returns a sequence of pairs of sequential integers and values.  so this program is a little simpler if integer values is really what you need.

begin program.
vallabs=['bli','bla','blo','blu']
lablist = lablist=" ".join([str(j+1)+" '" + k +"'" for j,k in enumerate(vallabs)])
print lablist
end program.

Now to take this one step further, the following program uses an api from the spssaux.VariableDict class to generate and apply the value labels to the variable x.

data list free /x.
begin data
1
2
3
4
end data.

begin program.
import spss, spssaux
vallabs=['bli','bla','blo','blu']
vls = dict([(k+1,v) for k, v in enumerate(vallabs)])
print vls
vardict = spssaux.VariableDict()
vardict['x'].ValueLabels = vls
end program.

Final remark: if the value label text could contain a quote character, the value literals need to be "smart quoted".  There is a function called _smartquote in the spssaux module that will do this.

Regards,

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/12/2010 07:38 AM
Subject: [SPSSX-L] Help with ZIP command in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Dear all,

I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like

value labels 1"bli"2"bla"....

I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:

begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.


Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Ruben Geert van den Berg
In reply to this post by John F Hall
Dear John (and others),
 
First, lots and lots of thanks to Jon and Albert-Jan for being so helpful and patient!
 
Now, I want to do two things with this structure:
 
1) when you loop through a list of variables and catch the output with OMS, the variable tablenumber_ will correspond to these variables but it's not value labelled. Well, if Python will remember the list of variables anyway, it can also generate the appropriate value labels command. No manual typing of any value labels needed here ;-D
 
2) The same for reading sheets of an EXCEL workbook to datasets and using add files; I just discovered that the Python xlrd module can loop through the sheet names if you just specify the filename of the workbook (you don't even have to know how many sheets it contains or how they are named!). Again, no value label typing needed :^)
 
Best,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com



 

Date: Mon, 12 Jul 2010 21:14:57 +0200
From: [hidden email]
Subject: Re: Help with ZIP command in Python
To: [hidden email]

What's wrong with opening an syntax file and writing:
 
val lab
   <v1> '<label1>'
   <v12> '<label2>'
 
etc ???
 
You have to write the labels out anyway and this keeps the syntax for future reference and amending if necessary.
----- Original Message -----
Sent: Monday, July 12, 2010 3:34 PM
Subject: Help with ZIP command in Python

Dear all,
 
I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like
 
value labels 1"bli"2"bla"....
 
I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:
 
begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?
 
TIA!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.


New Windows 7: Simplify what you do everyday. Find the right PC for you.
Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Albert-Jan Roskam
Hi Ruben,
 
The xlrd and xlwt modules are very handy. Maybe you find the following program useful.
 
import xlrd, xlwt
import glob, os.path
 
def merge_xls(in_xls_file, out_xls_file, merged_book):
    book = xlrd.open_workbook(in_xls_file)
    root, ext = os.path.splitext(in_xls_file)
    sheet_basename = os.path.basename(root)
    msg = "Sheet names may not exceed 30 chars (%s)" % sheet_basename
    assert len(sheet_basename) <= 30, msg
    for sheetx in range(book.nsheets):
        sheet_name = sheet_basename + "-" + str(sheetx).zfill(2)
        ws = merged_book.add_sheet(sheet_name)
        sheet = book.sheet_by_index(sheetx)
        write_cells(sheet, ws)
    merged_book.save(out_xls_file)
 
def write_cells(sheet, ws):
    for rx in range(sheet.nrows):
        for cx in range(sheet.ncols):
            ws.write(rx, cx, sheet.cell_value(rx, cx))
 
def main(in_xls_files, out_xls_file):
    merged_book = xlwt.Workbook()
    for in_xls_file in in_xls_files:
        merge_xls(in_xls_file, out_xls_file, merged_book)
    print "Done! (%s)" % out_xls_file
main(out_xls_file = 'd:/merged_output.xls', \
     in_xls_files = glob.glob("d:/temp/*.xls"))


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 7/13/10, Ruben van den Berg <[hidden email]> wrote:

From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Help with ZIP command in Python
To: [hidden email]
Date: Tuesday, July 13, 2010, 12:08 PM

Dear John (and others),
 
First, lots and lots of thanks to Jon and Albert-Jan for being so helpful and patient!
 
Now, I want to do two things with this structure:
 
1) when you loop through a list of variables and catch the output with OMS, the variable tablenumber_ will correspond to these variables but it's not value labelled. Well, if Python will remember the list of variables anyway, it can also generate the appropriate value labels command. No manual typing of any value labels needed here ;-D
 
2) The same for reading sheets of an EXCEL workbook to datasets and using add files; I just discovered that the Python xlrd module can loop through the sheet names if you just specify the filename of the workbook (you don't even have to know how many sheets it contains or how they are named!). Again, no value label typing needed :^)
 
Best,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com



 

Date: Mon, 12 Jul 2010 21:14:57 +0200
From: [hidden email]
Subject: Re: Help with ZIP command in Python
To: [hidden email]

What's wrong with opening an syntax file and writing:
 
val lab
   <v1> '<label1>'
   <v12> '<label2>'
 
etc ???
 
You have to write the labels out anyway and this keeps the syntax for future reference and amending if necessary.
----- Original Message -----
Sent: Monday, July 12, 2010 3:34 PM
Subject: Help with ZIP command in Python

Dear all,
 
I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like
 
value labels 1"bli"2"bla"....
 
I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:
 
begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?
 
TIA!

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.


New Windows 7: Simplify what you do everyday. Find the right PC for you.

Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Jon K Peck
In reply to this post by Albert-Jan Roskam

See below/
Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Albert-Jan Roskam <[hidden email]>
To: [hidden email], Jon K Peck/Chicago/IBM@IBMUS
Date: 07/13/2010 01:45 AM
Subject: Re: [SPSSX-L] Help with ZIP command in Python





Hi Jon,
 
The last example you give is interesting. So the VariableLabel attribute is set by assigning to a new dictionary, right? Nice syntax to have vardict['x'].ValueLabels before the assignment operator.
But why can't the following be used to rename variables?

 
begin program.
import spss, spssaux
vardict = spssaux.VariableDict()
newlist = ["prefix_" + vardict[str(v)].VariableName for v in vardict]
#setattr(vardict, "VariableName", newlist)
vardict.VariableName = newlist
print vardict.VariableName
end program.


>>>There are several problems here.  vardict is a dictionary object, i.e., a container.  It does not have a VariableName property.
The objects in the dictionary have this property.

Second, the variable name is the key to an element in the dictionary (The position number can also be used).  So changing that would require updating a number of different dictionary structures.  The logical way to do it would be
vardict['x'].VariableName = 'y'
I made VariableName a read-only property, though, because of its use in various internal structures.  Something to consider as an enhancement.

Regards,
Jon


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Mon, 7/12/10, Jon K Peck <[hidden email]> wrote:


From: Jon K Peck <[hidden email]>
Subject: Re: [SPSSX-L] Help with ZIP command in Python
To: [hidden email]
Date: Monday, July 12, 2010, 4:20 PM


Here is a form that works.


begin program.

vallabs=['bli','bla','blo','blu']

lablist=" ".join([str(j)+" '" + k +"'" for j,k in zip(range(1, len(vallabs)+1),vallabs)])

print lablist

end program.


You need to generate a list with single string entries, so the j,k construct in the original does not work.

Also, while range generates a list that is appropriate for zipping, you need the list elements to be incremented inside the list construct.  Starting the range expression at 1 simplifies this.


But there is a specialized construct in Python for this sort of situation.  The enumerate function returns a sequence of pairs of sequential integers and values.  so this program is a little simpler if integer values is really what you need.


begin program.

vallabs=['bli','bla','blo','blu']

lablist = lablist=" ".join([str(j+1)+" '" + k +"'" for j,k in enumerate(vallabs)])

print lablist

end program.


Now to take this one step further, the following program uses an api from the spssaux.VariableDict class to generate and apply the value labels to the variable x.


data list free /x.

begin data

1

2

3

4

end data.


begin program.

import spss, spssaux

vallabs=['bli','bla','blo','blu']

vls = dict([(k+1,v) for k, v in enumerate(vallabs)])

print vls

vardict = spssaux.VariableDict()

vardict['x'].ValueLabels = vls

end program.


Final remark: if the value label text could contain a quote character, the value literals need to be "smart quoted".  There is a function called _smartquote in the spssaux module that will do this.


Regards,


Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Ruben van den Berg <[hidden email]>
To: [hidden email]
Date: 07/12/2010 07:38 AM
Subject: [SPSSX-L] Help with ZIP command in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>






Dear all,

I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like

value labels 1"bli"2"bla"....

I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:

begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?

TIA!


Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.



Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Jon K Peck
In reply to this post by Albert-Jan Roskam

The only caveat with xlrd and xlwt is that AFAIK they do not support the Excel 2007 file formats, so if you are using that version, you have to downsave the file.

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Albert-Jan Roskam <[hidden email]>
To: [hidden email]
Date: 07/13/2010 05:31 AM
Subject: Re: [SPSSX-L] Help with ZIP command in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi Ruben,
 
The xlrd and xlwt modules are very handy. Maybe you find the following program useful.
 
import xlrd, xlwt
import glob, os.path

 
def merge_xls(in_xls_file, out_xls_file, merged_book):
   book = xlrd.open_workbook(in_xls_file)
   root, ext = os.path.splitext(in_xls_file)
   sheet_basename = os.path.basename(root)
   msg = "Sheet names may not exceed 30 chars (%s)" % sheet_basename
   assert len(sheet_basename) <= 30, msg
   for sheetx in range(book.nsheets):
       sheet_name = sheet_basename + "-" + str(sheetx).zfill(2)
       ws = merged_book.add_sheet(sheet_name)
       sheet = book.sheet_by_index(sheetx)
       write_cells(sheet, ws)
   merged_book.save(out_xls_file)

 
def write_cells(sheet, ws):
   for rx in range(sheet.nrows):
       for cx in range(sheet.ncols):
           ws.write(rx, cx, sheet.cell_value(rx, cx))

 
def main(in_xls_files, out_xls_file):
   merged_book = xlwt.Workbook()
   for in_xls_file in in_xls_files:
       merge_xls(in_xls_file, out_xls_file, merged_book)
   print "Done! (%s)" % out_xls_file

main(out_xls_file = 'd:/merged_output.xls', \
    in_xls_files = glob.glob("d:/temp/*.xls"))


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 7/13/10, Ruben van den Berg <[hidden email]> wrote:


From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Help with ZIP command in Python
To: [hidden email]
Date: Tuesday, July 13, 2010, 12:08 PM

Dear John (and others),

First, lots and lots of thanks to Jon and Albert-Jan for being so helpful and patient!

Now, I want to do two things with this structure:

1) when you loop through a list of variables and catch the output with OMS, the variable tablenumber_ will correspond to these variables but it's not value labelled. Well, if Python will remember the list of variables anyway, it can also generate the appropriate value labels command. No manual typing of any value labels needed here ;-D

2) The same for reading sheets of an EXCEL workbook to datasets and using add files; I just discovered that the Python xlrd module can loop through the sheet names if you just specify the filename of the workbook (you don't even have to know how many sheets it contains or how they are named!). Again, no value label typing needed :^)

Best,

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





Date: Mon, 12 Jul 2010 21:14:57 +0200
From: [hidden email]
Subject: Re: Help with ZIP command in Python
To: [hidden email]

What's wrong with opening an syntax file and writing:
 
val lab
   <v1> '<label1>'
   <v12> '<label2>'
 
etc ???
 
You have to write the labels out anyway and this keeps the syntax for future reference and amending if necessary.
----- Original Message -----
From: Ruben van den Berg
To: SPSSX-L@...
Sent: Monday, July 12, 2010 3:34 PM
Subject: Help with ZIP command in Python

Dear all,

I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like

value labels 1"bli"2"bla"....

I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:

begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.


New Windows 7: Simplify what you do everyday. Find the right PC for you.



Reply | Threaded
Open this post in threaded view
|

Re: Help with ZIP command in Python

Ruben Geert van den Berg
Thanks once again! The entire syntax (pasted below) may not be elegant but it works as desired.
 
Best,

Ruben van den Berg
Consultant Models & Methods
TNS NIPO
Email: [hidden email]
Mobiel: +31 6 24641435
Telefoon: +31 20 522 5738
Internet: www.tns-nipo.com

*Python program for reading and merging EXCEL sheets and adding sheetnames as value labels to the merged dataset.
 

set mpr on.

begin program.

import xlrd,spss

from xlrd import open_workbook

wb=open_workbook('c:/temp/testbook.xls')

labs=[]

i=1

for s in wb.sheets():

 spss.Submit(r"""

 GET DATA

  /TYPE=XLS

  /FILE='C:\Temp\testbook.xls'

  /SHEET=name '%s'

  /CELLRANGE=full

  /READNAMES=on

  /ASSUMEDSTRWIDTH=32767.

 dataset name %s.

 compute sheet=%d.

 execute."""%(s.name, s.name,i))

 labs.append(s.name)

 i=i+1

vallabs=" ".join([str(j)+"'"+k+"'" for j,k in zip(range(1,len(labs)+1),labs)])

files="/file ".join(labs)

spss.Submit("""add files file %s.

execute.

value labels sheet %s.

dataset close all.

dataset name merged."""%(files,vallabs))

end program.

set mpr off.

 




 

Date: Tue, 13 Jul 2010 07:09:14 -0600
From: [hidden email]
Subject: Re: Help with ZIP command in Python
To: [hidden email]


The only caveat with xlrd and xlwt is that AFAIK they do not support the Excel 2007 file formats, so if you are using that version, you have to downsave the file.

Jon Peck
SPSS, an IBM Company
[hidden email]
312-651-3435



From: Albert-Jan Roskam <[hidden email]>
To: [hidden email]
Date: 07/13/2010 05:31 AM
Subject: Re: [SPSSX-L] Help with ZIP command in Python
Sent by: "SPSSX(r) Discussion" <[hidden email]>





Hi Ruben,
 
The xlrd and xlwt modules are very handy. Maybe you find the following program useful.
 
import xlrd, xlwt
import glob, os.path

 
def merge_xls(in_xls_file, out_xls_file, merged_book):
   book = xlrd.open_workbook(in_xls_file)
   root, ext = os.path.splitext(in_xls_file)
   sheet_basename = os.path.basename(root)
   msg = "Sheet names may not exceed 30 chars (%s)" % sheet_basename
   assert len(sheet_basename) <= 30, msg
   for sheetx in range(book.nsheets):
       sheet_name = sheet_basename + "-" + str(sheetx).zfill(2)
       ws = merged_book.add_sheet(sheet_name)
       sheet = book.sheet_by_index(sheetx)
       write_cells(sheet, ws)
   merged_book.save(out_xls_file)

 
def write_cells(sheet, ws):
   for rx in range(sheet.nrows):
       for cx in range(sheet.ncols):
           ws.write(rx, cx, sheet.cell_value(rx, cx))

 
def main(in_xls_files, out_xls_file):
   merged_book = xlwt.Workbook()
   for in_xls_file in in_xls_files:
       merge_xls(in_xls_file, out_xls_file, merged_book)
   print "Done! (%s)" % out_xls_file

main(out_xls_file = 'd:/merged_output.xls', \
    in_xls_files = glob.glob("d:/temp/*.xls"))


Cheers!!
Albert-Jan

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
All right, but apart from the sanitation, the medicine, education, wine, public order, irrigation, roads, a fresh water system, and public health, what have the Romans ever done for us?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

--- On Tue, 7/13/10, Ruben van den Berg <[hidden email]> wrote:


From: Ruben van den Berg <[hidden email]>
Subject: Re: [SPSSX-L] Help with ZIP command in Python
To: [hidden email]
Date: Tuesday, July 13, 2010, 12:08 PM

Dear John (and others),

First, lots and lots of thanks to Jon and Albert-Jan for being so helpful and patient!

Now, I want to do two things with this structure:

1) when you loop through a list of variables and catch the output with OMS, the variable tablenumber_ will correspond to these variables but it's not value labelled. Well, if Python will remember the list of variables anyway, it can also generate the appropriate value labels command. No manual typing of any value labels needed here ;-D

2) The same for reading sheets of an EXCEL workbook to datasets and using add files; I just discovered that the Python xlrd module can loop through the sheet names if you just specify the filename of the workbook (you don't even have to know how many sheets it contains or how they are named!). Again, no value label typing needed :^)

Best,

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





Date: Mon, 12 Jul 2010 21:14:57 +0200
From: [hidden email]
Subject: Re: Help with ZIP command in Python
To: [hidden email]

What's wrong with opening an syntax file and writing:
 
val lab
   <v1> '<label1>'
   <v12> '<label2>'
 
etc ???
 
You have to write the labels out anyway and this keeps the syntax for future reference and amending if necessary.
----- Original Message -----
From: Ruben van den Berg
To: SPSSX-L@...
Sent: Monday, July 12, 2010 3:34 PM
Subject: Help with ZIP command in Python

Dear all,

I've a list of variable labels that correspond to values 1, 2, ..., n. If possible, I'd like to apply them with a single command, like

value labels 1"bli"2"bla"....

I'm trying to built this list with the ZIP function in Python but I don't know how to do it. My attempt so far (for just this part) is:

begin program.
vallabs=['bli','bla','blo','blu']
lablist='"'.join([j,k for j,k in zip(range(len(vallabs))+1,vallabs)])
print lablist
end program.

Could anyone please point out how this could be done?

TIA!

Ruben van den Berg

Consultant Models & Methods

TNS NIPO

Email: [hidden email]

Mobiel: +31 6 24641435

Telefoon: +31 20 522 5738

Internet:
www.tns-nipo.com





New Windows 7: Find the right PC for you. Learn more.


New Windows 7: Simplify what you do everyday. Find the right PC for you.





Express yourself instantly with MSN Messenger! MSN Messenger