[Tutor] Question about writing to Excel with slavic characters

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

[Tutor] Question about writing to Excel with slavic characters

Marko Limbek
Hi everyone.


I am new to list and few months old to Python. I am writing some text
to Excel and I open the new book and try to write to the book and the
save it using

book.save

Now when I write slavic characters in the text to Excel (č, š, ž, for
instance 0xc5), I get an error, I can't save it.
I have declared appropriate encoding

# -*- coding: utf-8 -*-
# coding=<utf-8>
#!/E:/Python

and those characters appear normally in the code, but there seems to
be the problem with the function book.save.
Does anyone have any ideas, what would be the problem and how to solve
it? Some additional encoding or some changes or parameters to the
book.save method?

Is that the right forum for my question?


Thank you,

Marko
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Christian Witts
On 2012/03/05 02:37 PM, Marko Limbek wrote:
Hi everyone.


I am new to list and few months old to Python. I am writing some text
to Excel and I open the new book and try to write to the book and the
save it using

book.save

Now when I write slavic characters in the text to Excel (č, š, ž, for
instance 0xc5), I get an error, I can't save it.
I have declared appropriate encoding

# -*- coding: utf-8 -*-
# coding=<utf-8>
#!/E:/Python

and those characters appear normally in the code, but there seems to
be the problem with the function book.save.
Does anyone have any ideas, what would be the problem and how to solve
it? Some additional encoding or some changes or parameters to the
book.save method?

Is that the right forum for my question?


Thank you,

Marko
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
What package are you using to create your Excel workbook ?
If it's xlwt you can set your encoding type when you create your workbook
book = xlwt.Workbook(encoding="utf-8")
--
Email Signature

Christian Witts
Python Developer


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Marko Limbek
Thank you!

That was easy. Now I have another problem.
I use RPy and read the spss database with method read.spss inside a
nested R code in Python, that looks like that:


import rpy

r("""

library(foreign)

baza <- read.spss(""" + analysis[0] + """)

print(baza$demo_izob0)

""")

Now when my text data labels in spss have slavic characters, they are
not recognised and output is something like that:

stiriletna srednja �ola
nedokon�ana osnovna �ola


What should I do here?


Thanks a lot,


Marko




On Mon, Mar 5, 2012 at 1:46 PM, Christian Witts <[hidden email]> wrote:

> On 2012/03/05 02:37 PM, Marko Limbek wrote:
>
> Hi everyone.
>
>
> I am new to list and few months old to Python. I am writing some text
> to Excel and I open the new book and try to write to the book and the
> save it using
>
> book.save
>
> Now when I write slavic characters in the text to Excel (č, š, ž, for
> instance 0xc5), I get an error, I can't save it.
> I have declared appropriate encoding
>
> # -*- coding: utf-8 -*-
> # coding=<utf-8>
> #!/E:/Python
>
> and those characters appear normally in the code, but there seems to
> be the problem with the function book.save.
> Does anyone have any ideas, what would be the problem and how to solve
> it? Some additional encoding or some changes or parameters to the
> book.save method?
>
> Is that the right forum for my question?
>
>
> Thank you,
>
> Marko
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> What package are you using to create your Excel workbook ?
> If it's xlwt you can set your encoding type when you create your workbook
> book = xlwt.Workbook(encoding="utf-8")
> --
>
> Christian Witts
> Python Developer
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Christian Witts
On 2012/03/05 03:05 PM, Marko Limbek wrote:
Thank you!

That was easy. Now I have another problem.
I use RPy and read the spss database with method read.spss inside a
nested R code in Python, that looks like that:


import rpy

r("""

library(foreign)

baza <- read.spss(""" + analysis[0] + """)

print(baza$demo_izob0)

""")

Now when my text data labels in spss have slavic characters, they are
not recognised and output is something like that:

stiriletna srednja �ola
nedokon�ana osnovna �ola


What should I do here?


Thanks a lot,


Marko




On Mon, Mar 5, 2012 at 1:46 PM, Christian Witts [hidden email] wrote:
On 2012/03/05 02:37 PM, Marko Limbek wrote:

Hi everyone.


I am new to list and few months old to Python. I am writing some text
to Excel and I open the new book and try to write to the book and the
save it using

book.save

Now when I write slavic characters in the text to Excel (č, š, ž, for
instance 0xc5), I get an error, I can't save it.
I have declared appropriate encoding

# -*- coding: utf-8 -*-
# coding=<utf-8>
#!/E:/Python

and those characters appear normally in the code, but there seems to
be the problem with the function book.save.
Does anyone have any ideas, what would be the problem and how to solve
it? Some additional encoding or some changes or parameters to the
book.save method?

Is that the right forum for my question?


Thank you,

Marko
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor

What package are you using to create your Excel workbook ?
If it's xlwt you can set your encoding type when you create your workbook
book = xlwt.Workbook(encoding="utf-8")
--

Christian Witts
Python Developer


Hi, you should avoid top-posting as it makes it hard to follow the thread if people have to bounce between the top and bottom of the post.

As for the answer to your question, I would suggest the R Mailing List if they have any issues with Unicode or need any encodings set when loading data etc.
There was a bug with Unicode support & RPy <2.2 but that was fixed last year so if you have a recent version of RPy that shouldn't be the issue.

--
Email Signature

Christian Witts
Python Developer


_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Steven D'Aprano-8
In reply to this post by Marko Limbek
Marko Limbek wrote:

> Thank you!
>
> That was easy. Now I have another problem.
> I use RPy and read the spss database with method read.spss inside a
> nested R code in Python, that looks like that:
>
>
> import rpy
>
> r("""
> library(foreign)
> baza <- read.spss(""" + analysis[0] + """)
> print(baza$demo_izob0)
> """)
>
> Now when my text data labels in spss have slavic characters, they are
> not recognised and output is something like that:
>
> stiriletna srednja �ola
> nedokon�ana osnovna �ola
>
>
> What should I do here?

Since the SPSS labels are being read by R, not Python, my guess is that this
is likely a problem with R, not Python.

You might find the rpy mailing list more helpful for solving rpy problems.

https://lists.sourceforge.net/lists/listinfo/rpy-list

Good luck!



--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Albert-jan Roskam
In reply to this post by Marko Limbek
Hi,

The R package foreign, in particular the read.spss function has some known problems reading spss system files. The memisc package also has a function to read .sav files. The big advantage of that function is that you can select rows and columns prior to actually reading the data into memory.

If you're just using R and Rpy to read .sav files, you could also consider using a Python program that I wrote a while ago:
http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/
It runs on Windows, Mac and Linux and requires no installation of spss. I'd be interested to hear your experiences.
 
Regards,
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?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 

From: Marko Limbek <[hidden email]>
To: [hidden email]
Cc: [hidden email]
Sent: Monday, March 5, 2012 2:05 PM
Subject: Re: [Tutor] Question about writing to Excel with slavic characters

Thank you!

That was easy. Now I have another problem.
I use RPy and read the spss database with method read.spss inside a
nested R code in Python, that looks like that:


import rpy

r("""

library(foreign)

baza <- read.spss(""" + analysis[0] + """)

print(baza$demo_izob0)

""")

Now when my text data labels in spss have slavic characters, they are
not recognised and output is something like that:

stiriletna srednja �ola
nedokon�ana osnovna �ola


What should I do here?


Thanks a lot,


Marko




On Mon, Mar 5, 2012 at 1:46 PM, Christian Witts <[hidden email]> wrote:

> On 2012/03/05 02:37 PM, Marko Limbek wrote:
>
> Hi everyone.
>
>
> I am new to list and few months old to Python. I am writing some text
> to Excel and I open the new book and try to write to the book and the
> save it using
>
> book.save
>
> Now when I write slavic characters in the text to Excel (č, š, ž, for
> instance 0xc5), I get an error, I can't save it.
> I have declared appropriate encoding
>
> # -*- coding: utf-8 -*-
> # coding=<utf-8>
> #!/E:/Python
>
> and those characters appear normally in the code, but there seems to
> be the problem with the function book.save.
> Does anyone have any ideas, what would be the problem and how to solve
> it? Some additional encoding or some changes or parameters to the
> book.save method?
>
> Is that the right forum for my question?
>
>
> Thank you,
>
> Marko
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
> What package are you using to create your Excel workbook ?
> If it's xlwt you can set your encoding type when you create your workbook
> book = xlwt.Workbook(encoding="utf-8")
> --
>
> Christian Witts
> Python Developer
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor



_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Marko Limbek
Hi,

I have been trying to use that programme but without success.
In Eclipse you usually create new project in two steps, first you
create new Pydev project, then inside you create new Pydev module.
I have created new project and new module 'crosstabs', where I want to
read spss file. My programme is the module.
Now I have put this ReaderWriter programme into the same Pydev project
as a another new module called 'rw' and then imported it into my
module 'crosstabs' with

import rw
from rw import SavReader

I don't even know, if I did that correctly. Actually I don't even know
how to implement that programme.
I put the recommended code

savFileName = "C:/dropbox/Exc_MarkoL_Zenel/Valicon/crosstabs/Tabela/ipk108_kosovo_data_finale_c1-1.sav"
with SavReader(savFileName) as sav:
    header = sav.next()
    for line in sav:
        process(line)


but I am get errors



I have however managed to compile the programme in the module, I just
had to comment more than 20 lines (because of that problem with
"employee data.sav" ).
I also don't know, if it was right to comment all those lines. Without
commenting them I can't even compile the module.


Maybe someone can tell me, how to actually implement that programme?
Where to put it in my Eclipse?


Is this programm ethe best way to read spss with Python?


Thank you so much,

Marko





On Tue, Mar 6, 2012 at 4:53 PM, Marko Limbek <[hidden email]> wrote:

> Hi Albert
>
> Two notes
> first I have used the promising looking package memics and the result
> is the same as foreign package. Reading in R is fine, but reading in
> rpy on Python27 unfortunatelly I get funny characters again like
> PRI�TINA
>
> second I have to run your programme and I get the following errors,
> that I send in attachment as printscreen. There is a file "employee
> data.sav" that I don't have and if I define my own file, I am asked in
> the next step about the variable "id" that I also don't have. So I
> would like to ask for your advise.
>
> Thanks,
> Marko
>
>
>
> On Mon, Mar 5, 2012 at 8:20 PM, Albert-Jan Roskam <[hidden email]> wrote:
>> Hi,
>>
>> The R package foreign, in particular the read.spss function has some known
>> problems reading spss system files. The memisc package also has a function
>> to read .sav files. The big advantage of that function is that you can
>> select rows and columns prior to actually reading the data into memory.
>>
>> If you're just using R and Rpy to read .sav files, you could also consider
>> using a Python program that I wrote a while ago:
>> http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/
>> It runs on Windows, Mac and Linux and requires no installation of spss. I'd
>> be interested to hear your experiences.
>>
>> Regards,
>> 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?
>> ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
>>
>> ________________________________
>> From: Marko Limbek <[hidden email]>
>> To: [hidden email]
>> Cc: [hidden email]
>> Sent: Monday, March 5, 2012 2:05 PM
>> Subject: Re: [Tutor] Question about writing to Excel with slavic characters
>>
>> Thank you!
>>
>> That was easy. Now I have another problem.
>> I use RPy and read the spss database with method read.spss inside a
>> nested R code in Python, that looks like that:
>>
>>
>> import rpy
>>
>> r("""
>>
>> library(foreign)
>>
>> baza <- read.spss(""" + analysis[0] + """)
>>
>> print(baza$demo_izob0)
>>
>> """)
>>
>> Now when my text data labels in spss have slavic characters, they are
>> not recognised and output is something like that:
>>
>> stiriletna srednja �ola
>> nedokon�ana osnovna �ola
>>
>>
>> What should I do here?
>>
>>
>> Thanks a lot,
>>
>>
>> Marko
>>
>>
>>
>>
>> On Mon, Mar 5, 2012 at 1:46 PM, Christian Witts <[hidden email]>
>> wrote:
>>> On 2012/03/05 02:37 PM, Marko Limbek wrote:
>>>
>>> Hi everyone.
>>>
>>>
>>> I am new to list and few months old to Python. I am writing some text
>>> to Excel and I open the new book and try to write to the book and the
>>> save it using
>>>
>>> book.save
>>>
>>> Now when I write slavic characters in the text to Excel (č, š, ž, for
>>> instance 0xc5), I get an error, I can't save it.
>>> I have declared appropriate encoding
>>>
>>> # -*- coding: utf-8 -*-
>>> # coding=<utf-8>
>>> #!/E:/Python
>>>
>>> and those characters appear normally in the code, but there seems to
>>> be the problem with the function book.save.
>>> Does anyone have any ideas, what would be the problem and how to solve
>>> it? Some additional encoding or some changes or parameters to the
>>> book.save method?
>>>
>>> Is that the right forum for my question?
>>>
>>>
>>> Thank you,
>>>
>>> Marko
>>> _______________________________________________
>>> Tutor maillist  -  [hidden email]
>>> To unsubscribe or change subscription options:
>>> http://mail.python.org/mailman/listinfo/tutor
>>>
>>> What package are you using to create your Excel workbook ?
>>> If it's xlwt you can set your encoding type when you create your workbook
>>> book = xlwt.Workbook(encoding="utf-8")
>>> --
>>>
>>> Christian Witts
>>> Python Developer
>> _______________________________________________
>> Tutor maillist  -  [hidden email]
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Steven D'Aprano-8
Marko Limbek wrote:

> I put the recommended code
>
> savFileName = "C:/dropbox/Exc_MarkoL_Zenel/Valicon/crosstabs/Tabela/ipk108_kosovo_data_finale_c1-1.sav"
> with SavReader(savFileName) as sav:
>     header = sav.next()
>     for line in sav:
>         process(line)
>
>
> but I am get errors


Will you tell us what errors, or should we just guess?

Since I love guessing games, let me try... my guess is that you get

AttributeError: 'SavReader' object has no attribute 'next'

Am I close? If so, try using next(sav) instead of sav.next().



> I have however managed to compile the programme in the module, I just
> had to comment more than 20 lines (because of that problem with
> "employee data.sav" ).
> I also don't know, if it was right to comment all those lines. Without
> commenting them I can't even compile the module.


Marko, it seems that you don't know how to program Python. Perhaps you should
do a Python tutorial or two so you can fix the code instead of just commenting
out lines.

Perhaps start here: http://docs.python.org/tutorial/


--
Steven
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Marko Limbek
On Wed, Mar 7, 2012 at 11:36 PM, Steven D'Aprano <[hidden email]> wrote:

> Marko Limbek wrote:
>
>> I put the recommended code
>>
>> savFileName =
>> "C:/dropbox/Exc_MarkoL_Zenel/Valicon/crosstabs/Tabela/ipk108_kosovo_data_finale_c1-1.sav"
>> with SavReader(savFileName) as sav:
>>    header = sav.next()
>>    for line in sav:
>>        process(line)
>>
>>
>> but I am get errors
>
>
>
> Will you tell us what errors, or should we just guess?
>
> Since I love guessing games, let me try... my guess is that you get
>
> AttributeError: 'SavReader' object has no attribute 'next'
>
> Am I close? If so, try using next(sav) instead of sav.next().


I overcame commenting. I managed to read my own file and print it. Now
I am trying to use method getNumberofVariables()  but unsuccesfully.
First way

SavReader(savFileName).getNumberofVariables()

gives me this error

Traceback (most recent call last):
  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
line 660, in <module>
    SavReader(savFileName).getNumberofVariables()
TypeError: getNumberofVariables() takes exactly 3 arguments (1 given)



Second way

getNumberofVariables(savFileName, fh, spssio)

gives me this error

Traceback (most recent call last):
  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
line 660, in <module>
    getNumberofVariables(savFileName, fh, spssio)
NameError: name 'getNumberofVariables' is not defined


Why there needs to be fh and spssio?
I would like to be able to know how to use all the methods to run and
test them and see, if any of them can give me labels from the file
instead of just pure numbers, before I start asking questions if there
is any method that can read labels. I am really sorry but I tried to
find answers to that in the tutorial.
I would like to read labels with such characters like čšž and not just
read pure numbers.


>
>
>> I have however managed to compile the programme in the module, I just
>> had to comment more than 20 lines (because of that problem with
>> "employee data.sav" ).
>> I also don't know, if it was right to comment all those lines. Without
>> commenting them I can't even compile the module.
>
>
>
> Marko, it seems that you don't know how to program Python. Perhaps you
> should do a Python tutorial or two so you can fix the code instead of just
> commenting out lines.
>
> Perhaps start here: http://docs.python.org/tutorial/
>


I have been using Python for a few months now and I am a
mathematician, so I have done some programming. It is true however
that I am not trained programmer. So please excuse dumb questions. But
I have fixed the code so far that I can read and print.

Marko
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

wprins
Hi Marko,

I'm going out on a limb here as I know next to nothing about either
SPSS or Albert-Jan's wrapper module, and so with that caveat, some
comments/observations:

On 8 March 2012 14:59, Marko Limbek <[hidden email]> wrote:

> I overcame commenting. I managed to read my own file and print it. Now
> I am trying to use method getNumberofVariables()  but unsuccesfully.
> First way
>
> SavReader(savFileName).getNumberofVariables()
>
> gives me this error
>
> Traceback (most recent call last):
>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
> line 660, in <module>
>    SavReader(savFileName).getNumberofVariables()
> TypeError: getNumberofVariables() takes exactly 3 arguments (1 given)
>
>
>
> Second way
>
> getNumberofVariables(savFileName, fh, spssio)
>
> gives me this error
>
> Traceback (most recent call last):
>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
> line 660, in <module>
>    getNumberofVariables(savFileName, fh, spssio)
> NameError: name 'getNumberofVariables' is not defined

The short answer:
==============
Don't use getNumberofVariables, use the script as demonstrated at the
bottom of the script in the "if __name__ == "__main__" section.  I
suppose something like this should do (I've modified it slightly to
make it a bit more obvious what's happening):

## ----- Get some basic file info
savFileName = r"C:\Program
Files\IBM\SPSS\Statistics\19\Samples\English\Employee data.sav"
mySavReaderObj = SavReader(savFileName)
numVars, nCases, varNames, varTypes, printTypesFile, printTypeLabels,
varWids = \
    mySavReaderObj.getSavFileInfo()
#Now the number of variables is presumably in numVars...


The longer answer:
==============
Firstly, getNumberofVariables() is defined as a class instance method.
 This means you can only ever call it on an object instance of that
class. It is not a standalone function that can be called freestanding
as you tried to do in your latter attempt.  That is why you got the
"not defined" error -- There is no freestanding function by that name
(even if there happens to be a /method/ by that name inside the
SavReader class.) so Python complained as such.

Your prior attempt, which as you noticed requires to have parameters
fh and spssio supplied appears to me to be effectively somewhat of an
internal/private/helper method that is written in the style of a
function, so consequently the method has no external dependencies on
any state in the object/class and hence has to have the spssio and fh
supplied when it's called.  You can see elsewhere in the class when
this method is called, the object itself keeps track of the fh and the
sspsio and so passes these into the method as required.

>From this you can infer that you'd be able to do the same and thus
successfully call getNumberofVariables() by retrieving the fh and
sspsio from your SavReader object (e.g. what I called mySavReaderObj
above), by passing in mySavReaderObj.fh and mySavREaderObj.spssio when
calling mySavReaderObj.getNumberofVariables().  But, as per the short
answer, you probably don't want to do that.

As an aside it also then follows it would be possible to
rewrite/refactor the getNumberofVariables() method (and indeed several
others) to remove the fh and sspsio parameters, by having them picked
up directly from the object instance when required.   Debateably, it
might be an improvement that makes the code a bit more object oriented
and less surprising to use.

HTH,

Walter
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Marko Limbek
Hi Walter

I understand, thank you. Maybe I am trying to do what is not meant to be done.
I tried as you suggested

mySavReaderObject = SavReader(savFileName)
mySavReaderObject.getNumberofVariables(savFileName,
mySavReaderObject.fh, mySavReaderObject.spssio)

but it won't work

  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
line 715, in <module>
    mySavReaderObject.getNumberofVariables(savFileName,
mySavReaderObject.fh, mySavReaderObject.spssio)
AttributeError: 'SavReader' object has no attribute 'spssio'

So the methods must really be somewhat internal.

The information that I get in
numVars, nCases, varNames, varTypes, printTypesFile, printTypeLabels, varWids
is not sufficient to me.

What I need from this programme are the labels that are in the column
'Values'  in SPSS 'Variable view'.
What is the most important is that unicode characters like 'č', 'š',
'ž', in the labels can be read. This is the only reason why I am
touching this programme.
I will forward the question to Albert.

Thank you,

Marko






On Thu, Mar 8, 2012 at 5:18 PM, Walter Prins <[hidden email]> wrote:

> Hi Marko,
>
> I'm going out on a limb here as I know next to nothing about either
> SPSS or Albert-Jan's wrapper module, and so with that caveat, some
> comments/observations:
>
> On 8 March 2012 14:59, Marko Limbek <[hidden email]> wrote:
>> I overcame commenting. I managed to read my own file and print it. Now
>> I am trying to use method getNumberofVariables()  but unsuccesfully.
>> First way
>>
>> SavReader(savFileName).getNumberofVariables()
>>
>> gives me this error
>>
>> Traceback (most recent call last):
>>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
>> line 660, in <module>
>>    SavReader(savFileName).getNumberofVariables()
>> TypeError: getNumberofVariables() takes exactly 3 arguments (1 given)
>>
>>
>>
>> Second way
>>
>> getNumberofVariables(savFileName, fh, spssio)
>>
>> gives me this error
>>
>> Traceback (most recent call last):
>>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
>> line 660, in <module>
>>    getNumberofVariables(savFileName, fh, spssio)
>> NameError: name 'getNumberofVariables' is not defined
>
> The short answer:
> ==============
> Don't use getNumberofVariables, use the script as demonstrated at the
> bottom of the script in the "if __name__ == "__main__" section.  I
> suppose something like this should do (I've modified it slightly to
> make it a bit more obvious what's happening):
>
> ## ----- Get some basic file info
> savFileName = r"C:\Program
> Files\IBM\SPSS\Statistics\19\Samples\English\Employee data.sav"
> mySavReaderObj = SavReader(savFileName)
> numVars, nCases, varNames, varTypes, printTypesFile, printTypeLabels,
> varWids = \
>    mySavReaderObj.getSavFileInfo()
> #Now the number of variables is presumably in numVars...
>
>
> The longer answer:
> ==============
> Firstly, getNumberofVariables() is defined as a class instance method.
>  This means you can only ever call it on an object instance of that
> class. It is not a standalone function that can be called freestanding
> as you tried to do in your latter attempt.  That is why you got the
> "not defined" error -- There is no freestanding function by that name
> (even if there happens to be a /method/ by that name inside the
> SavReader class.) so Python complained as such.
>
> Your prior attempt, which as you noticed requires to have parameters
> fh and spssio supplied appears to me to be effectively somewhat of an
> internal/private/helper method that is written in the style of a
> function, so consequently the method has no external dependencies on
> any state in the object/class and hence has to have the spssio and fh
> supplied when it's called.  You can see elsewhere in the class when
> this method is called, the object itself keeps track of the fh and the
> sspsio and so passes these into the method as required.
>
> >From this you can infer that you'd be able to do the same and thus
> successfully call getNumberofVariables() by retrieving the fh and
> sspsio from your SavReader object (e.g. what I called mySavReaderObj
> above), by passing in mySavReaderObj.fh and mySavREaderObj.spssio when
> calling mySavReaderObj.getNumberofVariables().  But, as per the short
> answer, you probably don't want to do that.
>
> As an aside it also then follows it would be possible to
> rewrite/refactor the getNumberofVariables() method (and indeed several
> others) to remove the fh and sspsio parameters, by having them picked
> up directly from the object instance when required.   Debateably, it
> might be an improvement that makes the code a bit more object oriented
> and less surprising to use.
>
> HTH,
>
> Walter
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

wprins
Hi Marko,

On 9 March 2012 08:34, Marko Limbek <[hidden email]> wrote:
>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
> line 715, in <module>
>    mySavReaderObject.getNumberofVariables(savFileName,
> mySavReaderObject.fh, mySavReaderObject.spssio)
> AttributeError: 'SavReader' object has no attribute 'spssio'
>
> So the methods must really be somewhat internal.

Python doesn't enforce access levels like some other languages do
which means effectively, any member of any object can in principle be
accessed.  By "gentlemans agreement", members with a name starting
with a single underscore are supposed to be considered private
(although you can still ignore this agreement and access them anyway)
while members with double underscores get some behind the scenes
assistance to ensure privateness and name uniqueness via "name
mangling".  (There's a couple of other minor behavioural differences,
but the point stands -- there's no preventing you as programmer from
accessing "private" members of a class if you insist to do so.  But
then it's doubly your problem if that gets you into trouble ;) )

Anyway, the message then really means what it says -- The SavReader
object instance you're using really does not have an spssio member
(there should be no problem accessing it if it was there so I must
interpret that message to mean what it says.)   I'm not sure why this
would be the case -- perhaps we're not looking/using the same version
of the reader class and the member name has changed?  (I previously
guessed/assumed  that you were using the following version, or
something close enough to it, given here:
http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/
  Looking back I see you're actually using a slightly newer version
from here: http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/
But, everything I've said appears to still hold true about the updated
version, so can you clarify which version you're currently using?)

Regardless, the error simple enough to troubleshoot -- have a look at
*your* version of the SavReader class, and find out what the member
name should in fact be.  There are calls to e.g.
getNumberofVariables() in the class itself, from which you can
determine what the object data/field/variable member is that holds the
value to pass to the spssio parameter of getNumberofVariables().

But, I have to repeat: The value that you get from
getNumberofVariables() is the exact same value that you get inside of
the numVars variable after calling:

  numVars, nCases, varNames, varTypes, printTypesFile,
printTypeLabels, varWids = mySavReaderObj.getSavFileInfo()

You can see this will be the case if you read the code:
1) The __init__ method assigns self.numVars_ from the result of
calling self._readBasicSavFileInfo().
2) This in turn calls on self.getNumberofVariables() as follows:
  numVars = self.getNumberofVariables(self.fh, self.spssio)[1]
... and that local variable numVars is what is returned and ends up in
the object member self.numVars_.
3) Then, looking at getSavFileInfo() you can see that it in turn
simply returns self.numVars_,
4) In other words it returns the same value that it previously
retrieved using self.getNumberofVariables().

So, the 2 ways are functionally identical w.r.t. the retrieval of the
"NumberofVariables".  The only differences are that a) with the latter
call you get a bunch of other stuff besides the number of variables,
and b) With the latter call you don't have to worry about spssio or fh
parameters (because they're absent/not required when calling
getSavFileInfo() and c) with the latter call the actual retrieval of
the number of variables happened slightly earlier on when the
SavReader object was created, while with the direct call to
getNumberofVariables() it is presumably read again directly from the
file.

So, I think you need to stop fixating on the getNumberofVariables()
method as it's not, I suspect, the solution to your real problem like
you seem to think, and it is also introducing a distraction (the
parameters issue) since it's not really the intended way for you to
use this class.  (Not that you absolutely cannot use it if you're
determined to do so, as I've already tried to explain, but it's just
probably just easier to go with the intended means of use for now
given that there's functionally no difference in the result up to this
point, at least that I can see.)

Walter
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor
Reply | Threaded
Open this post in threaded view
|

Re: [Tutor] Question about writing to Excel with slavic characters

Marko Limbek
Hi Walter

It is as you say. Thanks for long explanation.
I am using the newer version.
Now I also understand difference between single underscore and double
underscore. I would still have problems if I would want to programme
them for instance.

Well I always try to be independent and I want to answer the questions
by myself. Learning the tutorial by heart is also not my way. Maybe I
need just to understand it better and understand better the whole
philosophy of private and public methods. And what is __init__ method
and __enter__ method and so on.
I was just asking of that getNumberofVariables() method, because I
wanted to run the methods my self and see if I get the labels I wanted
by myself. I wouldn't want to ask the author directly does this
programme does this or that because that might be silly questions
because the answer would be, 'well of course it does, run this and
this method' and you will see. I don't want to consume too much time
of the people. So I wanted to check first. As an amateur as I am.
Nobody really trained me how to programme except for in the first
years at the faculty some 10 years ago, but at that time I was not
that interested in programming.

Now we are resolving the issue directly with Albert.

Have a nice day,
Marko


On Fri, Mar 9, 2012 at 11:49 AM, Walter Prins <[hidden email]> wrote:

> Hi Marko,
>
> On 9 March 2012 08:34, Marko Limbek <[hidden email]> wrote:
>>  File "C:\Dropbox\Exc_MarkoL_Zenel\Python\crosstabs\src\src\rw.py",
>> line 715, in <module>
>>    mySavReaderObject.getNumberofVariables(savFileName,
>> mySavReaderObject.fh, mySavReaderObject.spssio)
>> AttributeError: 'SavReader' object has no attribute 'spssio'
>>
>> So the methods must really be somewhat internal.
>
> Python doesn't enforce access levels like some other languages do
> which means effectively, any member of any object can in principle be
> accessed.  By "gentlemans agreement", members with a name starting
> with a single underscore are supposed to be considered private
> (although you can still ignore this agreement and access them anyway)
> while members with double underscores get some behind the scenes
> assistance to ensure privateness and name uniqueness via "name
> mangling".  (There's a couple of other minor behavioural differences,
> but the point stands -- there's no preventing you as programmer from
> accessing "private" members of a class if you insist to do so.  But
> then it's doubly your problem if that gets you into trouble ;) )
>
> Anyway, the message then really means what it says -- The SavReader
> object instance you're using really does not have an spssio member
> (there should be no problem accessing it if it was there so I must
> interpret that message to mean what it says.)   I'm not sure why this
> would be the case -- perhaps we're not looking/using the same version
> of the reader class and the member name has changed?  (I previously
> guessed/assumed  that you were using the following version, or
> something close enough to it, given here:
> http://code.activestate.com/recipes/577650-python-reader-for-spss-sav-files/
>  Looking back I see you're actually using a slightly newer version
> from here: http://code.activestate.com/recipes/577811-python-reader-writer-for-spss-sav-files-linux-mac-/
> But, everything I've said appears to still hold true about the updated
> version, so can you clarify which version you're currently using?)
>
> Regardless, the error simple enough to troubleshoot -- have a look at
> *your* version of the SavReader class, and find out what the member
> name should in fact be.  There are calls to e.g.
> getNumberofVariables() in the class itself, from which you can
> determine what the object data/field/variable member is that holds the
> value to pass to the spssio parameter of getNumberofVariables().
>
> But, I have to repeat: The value that you get from
> getNumberofVariables() is the exact same value that you get inside of
> the numVars variable after calling:
>
>  numVars, nCases, varNames, varTypes, printTypesFile,
> printTypeLabels, varWids = mySavReaderObj.getSavFileInfo()
>
> You can see this will be the case if you read the code:
> 1) The __init__ method assigns self.numVars_ from the result of
> calling self._readBasicSavFileInfo().
> 2) This in turn calls on self.getNumberofVariables() as follows:
>  numVars = self.getNumberofVariables(self.fh, self.spssio)[1]
> ... and that local variable numVars is what is returned and ends up in
> the object member self.numVars_.
> 3) Then, looking at getSavFileInfo() you can see that it in turn
> simply returns self.numVars_,
> 4) In other words it returns the same value that it previously
> retrieved using self.getNumberofVariables().
>
> So, the 2 ways are functionally identical w.r.t. the retrieval of the
> "NumberofVariables".  The only differences are that a) with the latter
> call you get a bunch of other stuff besides the number of variables,
> and b) With the latter call you don't have to worry about spssio or fh
> parameters (because they're absent/not required when calling
> getSavFileInfo() and c) with the latter call the actual retrieval of
> the number of variables happened slightly earlier on when the
> SavReader object was created, while with the direct call to
> getNumberofVariables() it is presumably read again directly from the
> file.
>
> So, I think you need to stop fixating on the getNumberofVariables()
> method as it's not, I suspect, the solution to your real problem like
> you seem to think, and it is also introducing a distraction (the
> parameters issue) since it's not really the intended way for you to
> use this class.  (Not that you absolutely cannot use it if you're
> determined to do so, as I've already tried to explain, but it's just
> probably just easier to go with the intended means of use for now
> given that there's functionally no difference in the result up to this
> point, at least that I can see.)
>
> Walter
> _______________________________________________
> Tutor maillist  -  [hidden email]
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist  -  [hidden email]
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor