Re: [Tutor] inserting new lines in long strings while printing
Abhishek Pratap wrote: I have this one big string in python which I want to print to a file inserting a new line after each 100 characters. Is there a slick way to do this without looping over the string. I am pretty sure there shud be something its just I am new to the lang. There is also textwrap.fill(). It honours word boundaries to some extent, so there may be lines that are shorter than the specified width. import textwrap print textwrap.fill(dere gewizzede bizz + a*100, width=10) dere gewizzede bizz a aa aa aa aa aa aa aa aa aa a ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE problems
OK, so from Finder. In that case can you open a Terminal window and drag the python folder into the Terminal - that should navigate you to the same folder. Then try running IDLE by typing python ./idle.py At the terminal prompt. I meant to say that this will let you see any error messages from running IDLE. If you get any, post them here. Here's what I get: Last login: Wed Mar 7 11:27:19 on ttys000 MacBook-001FF3569577-4:~ fedka2$ /Applications/Python\ 3.2 -bash: /Applications/Python 3.2: is a directory MacBook-001FF3569577-4:~ fedka2$ python ./idle.py /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file './idle.py': [Errno 2] No such file or directory MacBook-001FF3569577-4:~ fedka2$ I don't seem to be getting a terminal prompt after dragging the folder into the shell?? Thanks again Paul ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] inserting new lines in long strings while printing
From: Abhishek Pratap abhishek@gmail.com To: Steven D'Aprano st...@pearwood.info Cc: tutor@python.org Sent: Wednesday, March 7, 2012 2:56 AM Subject: Re: [Tutor] inserting new lines in long strings while printing thanks guys .. -Abhi On Tue, Mar 6, 2012 at 5:41 PM, Steven D'Aprano st...@pearwood.info wrote: On Tue, Mar 06, 2012 at 05:26:26PM -0800, Abhishek Pratap wrote: I have this one big string in python which I want to print to a file inserting a new line after each 100 characters. Is there a slick way to do this without looping over the string. I am pretty sure there shud be something its just I am new to the lang. s = a*100 print '\n'.join(s[i:i+10] for i in range(0, len(s), 10)) aa aa aa aa aa aa aa aa aa aa Hi, using the textwrap module is also an option: import textwrap s = a*100 print \n.join(textwrap.wrap(s, 10)) aa aa aa aa aa aa aa aa aa aa ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE problems
Sorry, I was assuming too much knowledge I suspect. Last login: Wed Mar 7 11:27:19 on ttys000 MacBook-001FF3569577-4:~ fedka2$ /Applications/Python\ 3.2 This line is the prompt plus the folder that you dragged :- /Applications/Python\ 3.2 You needed to type cd first (no RETURN just cd) - I took that step for granted, sorry. then drop the folder But since we now know the folder name you could just type cd /Applications/Python\ 3.2 That should give you a terminal prompt of: MacBook-001FF3569577-4: /Applications/Python 3.2$ If that is where idle.py is? I would expect it to be lower down than that... Then you can type python ./idle.py If, as I suspect, idle.py is lower in the folder structure you will need to modify things by providing the path to idle.py I hope that's clearer. One of the barriers to programming on a Mac for most users is that they pretty much have to learn how to drive the Terminal. And that's kind of a counter-culture to Mac users. Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE problems
Thanks again, I'm going to push my luck and ask you a final question on this subject, you've been really helpful. Should I be able to locate idle.py with a spotlight search. I can't seem to locate it... Here is what terminal gave me MacBook-001FF3569577-4:~ fedka2$ cd /Applications/Python\ 3.2 MacBook-001FF3569577-4:Python 3.2 fedka2$ python ./idle.py /Library/Frameworks/Python.framework/Versions/2.7/Resources/Python.app/Contents/MacOS/Python: can't open file './idle.py': [Errno 2] No such file or directory MacBook-001FF3569577-4:Python 3.2 fedka2$ Thanks again, and here's to the hillwalking (I'm in the French Jura, so it is no stranger to me) Paul On Wed, Mar 7, 2012 at 12:30 PM, ALAN GAULD alan.ga...@btinternet.com wrote: Sorry, I was assuming too much knowledge I suspect. Last login: Wed Mar 7 11:27:19 on ttys000 MacBook-001FF3569577-4:~ fedka2$ /Applications/Python\ 3.2 This line is the prompt plus the folder that you dragged :- /Applications/Python\ 3.2 You needed to type cd first (no RETURN just cd) - I took that step for granted, sorry. then drop the folder But since we now know the folder name you could just type cd /Applications/Python\ 3.2 That should give you a terminal prompt of: MacBook-001FF3569577-4: /Applications/Python 3.2$ If that is where idle.py is? I would expect it to be lower down than that... Then you can type python ./idle.py If, as I suspect, idle.py is lower in the folder structure you will need to modify things by providing the path to idle.py I hope that's clearer. One of the barriers to programming on a Mac for most users is that they pretty much have to learn how to drive the Terminal. And that's kind of a counter-culture to Mac users. Alan G ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about writing to Excel with slavic characters
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 marko.lim...@valicon.net 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 fo...@yahoo.com 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 marko.lim...@valicon.net To: cwi...@compuscan.co.za Cc: tutor@python.org 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 cwi...@compuscan.co.za 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 - Tutor@python.org 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
Re: [Tutor] misunderstanding any
a = tuple(range(10)) b = tuple(reversed(a)) any(a) in b True any(b) in a True any((a,b)) in (a,b) False # I think I understand this now, but I must admit it looks confusing! I just want to clarify some things. 'any(a) in b' evaluates any(a) before it evaluates 'x in b'. So in essence 'any(a) in b' is equivalent to 'True in b'. The reason why you get True in the first two cases is because of Python's history where there were no bool types (I could be wrong on this). At the very least bool types inherit from integer in a common practice from older languages like C. Hopefully some of my samples will help explain my point. a = range(10) b = tuple( reversed( a ) ) any(a) # Any non-zero value equates to true True any(a) in b # True == 1 True True in b # True == 1 True any(b) # Any non-zero value equates to true True True in a # True == 1 True True == 1 # Proof that True == 1 True False == 0 # Proof that False == 0 True any( (a,b) ) # many non-zero values True a = range( 10,20 ) # create a list without 1 b = tuple( reversed( a ) ) any( a ) # still contains non-zero values True any( a ) in b # b is missing the value 1 and therefore missing True False any( b ) # still contains non-zero values True any (b ) in a # a is missing the value 1 and therefore missing True False any( (a,b) ) # still contains non-zero values True any ( (a,b) ) in ( a,b ) # a and b is missing the value 1 False any ( (a,b) ) in ( a,b, 1 ) # 1 == True True Thinking that if *any* of the tuples is in fruit_type(a list of tuples), then it should return True. What you want is not any, but probably filter or any + map. c = zip( a, b ) c [(10, 19), (11, 18), (12, 17), (13, 16), (14, 15), (15, 14), (16, 13), (17, 12), (18, 11), (19, 10)] d = ( 11, 18 ) filter( lambda x: d == x, c ) [(11, 18)] if filter( lambda x: d == x, c ): ... print 'True' ... True map( lambda x: x==d, c ) [False, True, False, False, False, False, False, False, False, False] any( map( lambda x: x==d, c ) ) True If you are comparing a list of tuples against a list of tuples merely change the above '==' to 'in' and d to be the second list of tuples and it should have similar results Ramit Ramit Prasad | JPMorgan Chase Investment Bank | Currencies Technology 712 Main Street | Houston, TX 77002 work phone: 713 - 216 - 5423 -- This email is confidential and subject to important disclaimers and conditions including on offers for the purchase or sale of securities, accuracy and completeness of information, viruses, confidentiality, legal privilege, and legal entity disclaimers, available at http://www.jpmorgan.com/pages/disclosures/email. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] misunderstanding any
On 03/07/2012 12:07 AM, col speed wrote: snip Then we have: a = tuple(range(10)) b = tuple(reversed(a)) any(a) in b True any(b) in a True any((a,b)) in (a,b) False # I think I understand this now, but I must admit it looks confusing! Thanks again Col None of those last three does anything usefully similar to your original request. Just because an English phrase makes sense, you can't expect the equivalent set of keywords to do anything remotely related. If you insist on using the any function in solving your problem, you'll have to preprocess your two arrays into a single iterator. And at that point, you might as well solve the problem in that loop. (untested): def test(a, b): for val1 in a: for val2 in b: if val1 == val2: return True return False print test(a,b) Rather than: def findinter(a, b): res = [] for val1 in a: for val2 in b: res.append(val1 == val2) return res print any(findinter(a,b)) -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] IDLE problems FIXED
Thanks Alan, It seems like it was a keybinding issue. When I dragged IDLE into a terminal window I got a laundry list of errors having to do with correct keys. I switched to a built in key set classic mac, and I'm running. I really appreciated your help with this. Paul On Wed, Mar 7, 2012 at 12:25 AM, Alan Gauld alan.ga...@btinternet.com wrote: On 06/03/12 18:12, ALAN GAULD wrote: OK, so from Finder. In that case can you open a Terminal window and drag the python folder into the Terminal - that should navigate you to the same folder. Then try running IDLE by typing python ./idle.py At the terminal prompt. I meant to say that this will let you see any error messages from running IDLE. If you get any, post them here. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Question about writing to Excel with slavic characters
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 - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] misunderstanding any
On 8 March 2012 01:11, Dave Angel d...@davea.name wrote: On 03/07/2012 12:07 AM, col speed wrote: snip Then we have: a = tuple(range(10)) b = tuple(reversed(a)) any(a) in b True any(b) in a True any((a,b)) in (a,b) False # I think I understand this now, but I must admit it looks confusing! Thanks again Col None of those last three does anything usefully similar to your original request. Just because an English phrase makes sense, you can't expect the equivalent set of keywords to do anything remotely related. If you insist on using the any function in solving your problem, you'll have to preprocess your two arrays into a single iterator. And at that point, you might as well solve the problem in that loop. (untested): def test(a, b): for val1 in a: for val2 in b: if val1 == val2: return True return False print test(a,b) Rather than: def findinter(a, b): res = [] for val1 in a: for val2 in b: res.append(val1 == val2) return res print any(findinter(a,b)) -- DaveA Thanks again to everybody for your very clear explanations. I have done away with any and have defined a function that iterates through one list and returns True if *i* in the next . It seems to work every time and also looks a little clearer. Cheers Col ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor