|
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. |
|
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
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. |
|
In reply to this post by Ruben Geert van den Berg
|
|
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.
|
|
In reply to this post by Jon K Peck
|
|
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.
New Windows 7: Simplify what you do everyday. Find the right PC for you. |
|
|
In reply to this post by Albert-Jan Roskam
See below/ Jon Peck SPSS, an IBM Company [hidden email] 312-651-3435
|
|
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
|
|
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
Express yourself instantly with MSN Messenger! MSN Messenger |
| Free forum by Nabble | Edit this page |
