Re: [Tutor] Even More Converter!
Kepala Pening wrote: import re num = 123456789 print ','.join(re.findall(\d{3}, str(num))) output: 123,456,789 [snip] The problem with that is that it cuts the digits in the end of the number, if they can't form a 3 digit value. Example: import re n = 1234 print ,.join(re.findall(\d{3}, str(n))) Output: 123, instead of 1,234 I think the use of a function would be better. def convert_num(num): num = map(lambda x: int(x), str(num)) num.reverse() k=0 tmp_number = [] for i in range(len(num)): if k == 2 and i != range(len(num))[-1]: tmp_number.append(num[i]) tmp_number.append(,) k = 0 else: tmp_number.append(num[i]) k += 1 num = map(lambda n: str(n), tmp_number) num.reverse() num = .join(num) return num First it converts the number into a list in which each digit is a separate member. Then it reverses that list (because when we want to add the commas, we start to count from the right and not from the left, that is, it's 1,234 and not 123,4). Next a few loop variables (k and tmp_number), and we loop through the num list, appending it's reversed digits into tmp_number, except when we have added 2 numbers without adding a comma, so we append the next number AND a comma. When the cycle ends, tmp_number is a list of ints with , string separating groups of 3 numbers. In the end, it make num the same as tmp_number, with all it's members turned to strings (didn't knew that join() only worked with strings in a list), reverse it (so it returns the same number that was in the beginning, and joins everything into a string. Example: n = 1234 convert_num(n) Output: 1,234 -- _ ASCII ribbon campaign ( ) - against HTML email X vCards / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Even More Converter!
Alan Gauld wrote: If that is important you might need to investigate a locale specific way of defining the seperator. I know Windows has hooks to get it from the local settings but I'm not sure about *nix and I don't know if Python has a generic way. This might not matter to you in practice , but I thought I'd mention it just in case... There is always the shell command locale. ___ import subprocess output = subprocess.Popen(locale | grep NUMERIC, shell=True, \ stdout=subprocess.PIPE) print output.communicate()[0] LC_NUMERIC=pt_PT.UTF-8 (In my case) ___ The problem with this is that it assumes the default shell is properly setted up (which may not be the case, for example this output happens in Bash, but in ZSH it gives en_US.UTF-8), which may not be the case. -- _ ASCII ribbon campaign ( ) - against HTML email X vCards / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] getting filen basename without extension
Timmie wrote: Hello, I would like to get the name of a file without it's extension/suffix. What is the easiest and fastes way to get the basename of a file wihout extension? What I found is this: import os myfile_name_with_path = 'path/to/my/testfile.txt' basename = os.path.basename(myfile_with_path) filename = os.path.splitext(basename) myfile_name_without_suffix = filename[0] Can this be done with less code? Thanks and kind regards, Timmie ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I did something like this: import os path = 'path/to/file.ext' filename = path.split(.)[0].split(/)[-1] print filename file The only problem I see is if the file has some . character besides the one before the extention. -- _ ASCII ribbon campaign ( ) - against HTML email X vCards / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The name of the module
János Juhász wrote: Dear Tutors, there was a thread some weeks ago about how can we find out what is the name of the current module, where the function was loaded from, where the function running from or so, with some magic. I can't find it in the archive. May someone help me with some reference about it ? Yours sincerely, __ János Juhász ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor I can only find something back in February of this year. http://mail.python.org/pipermail/tutor/2007-February/052914.html Don't know if that's what you're after though. -- _ ASCII ribbon campaign ( ) - against HTML email X vCards / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to Practice Python?(Linpeiheng)
Alan Gauld wrote: There are no such things as standard solutions to programming problems, its not like doing math! But usually there is The Right Way. I think... -- _ ASCII ribbon campaign ( ) - against HTML email X vCards / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hi,every one
Yang Yang escreveu: i am a newman for python world i have some word want to ask 1.what is the best book for python study. I like Dive into Python. ( http://www.diveintopython.org/ ) 2.what's is the better IDE for python That depends on what OS you are. Thanks for all ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- _ ASCII ribbon campaign ( ) - against HTML email X vCards / \ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] two input acceptions
adam urbas escreveu: Thanks for the help. I've made quite some progress since I first posted this email. I have a question though, what did you mean when you were talking about the raw_input( )? How can the regular input( ) be used evilly? If you could explain in depth, I would be very grateful. I have a new question related to my program area.py., I guess it's the same one as before. When I run the program and input the rectangle option, it asks me for a radius, unless I input 1, instead of rectangle. How do I program it to accept both 1 and rectangle? Date: Sat, 12 May 2007 18:55:20 +0100 From: [EMAIL PROTECTED] To: [EMAIL PROTECTED] CC: tutor@python.org Subject: Re: [Tutor] (no subject) adam urbas escreveu: Hi,I just started python today and I would like a few pointers, if you don't mind. I tried using a tutorial, but was only able to get the correct results for the most basic problems. # Area calculation programprint “Welcome to the Area calculation program”print “–– –––”print# Print out the menu:print “Please select a shape:”print “1 Rectangle”print “2 Circle”# Get the user’s choice:shape = input(“ “)# Calculate the area:if shape == 1:height = input(“Please enter the height: “)width = input(“Please enter the width: “)area = height*widthprint “The area is”, areaelse:radius = input(“Please enter the radius: “)area = 3.14*(radius**2)print “The area is”, areaI've been trying to get this to work. I was on a forum on Google and they said to put:input(press ENTER to continue)at the end. I did, but it didn't work. It runs the program but just shuts itself off when its done and i don't even get to select any of the option things that i'm s upposed to be able to select. It just turns on then back off and I don't even get to see anything. Could someone help me out.ThanksAdam _ Create the ultimate e-mail address book. Import your cont acts to Windows Live Hotmail. www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-usocid=TXT_TAGLM_HMWL_reten_impcont_0507 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor First, welcome to the world of Python. :D Second. please give a title when you start a new thread on a mailing list. Third, format your posts and code. Since Python uses indented code, it's kinda hard to read it when it's all in one line (Don't worry, I'll paste it indented in a file attached to this email :D ) Now for the code. After arranging the code, the first thing I noticed were this characters “ ” I tried running the code, and if gave me a error there, so I just replace then with , and voilá, the code worked :D . So the lesson here is always use either or ' ' in the code. Oh, a lso another thing. Don't use input() to get the user input, because that command can run code and it may be evilly used. Always use raw_input() instead :D . Anyway, I hope I helped you, -- _ ASCII ribbon campaign ( ) - against HTML email XvCards / \ _ Create the ultimate e-mail address book. Import your contacts to Windows Live Hotmail. www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-usocid=TXT_TAGLM_HMWL_reten_impcont_0507 First of all, what email client are you using? Because the text is getting all weird and difficult to read (it's all in one line, with no paragraphs and things like that). Now, the thing about input() and why it's not a good policy to use is that, unlike raw_input(), what type in a input() is executed by Python (in raw_input() is stored as a string). Example: var = raw_input() list(LOL) Now we have a variable called var which contains the string that says 'list(LOL)' You can confirm that by typing: print var 'list(LOL) There, no harm done. Now let's try the same thing using the input() command: var = input() list(LOL) Now let's type print var again as we did before. print var ['L', 'O'. 'L'] Now what happened? Because you used the input() command, what you type was interpreted by Python, instead of being stored in a string and since the list() command is used to create a list, Python did just that. He created a list. Now, in this example, no harm was done. But image someone typing the command os.system(command to delete some file or run some file). That would send a delete command to the terminal, or install some file (it could even be a virus). Ok, it's a little harder to explain, but the thing you should is that usually raw_input() = GOOD, input() = BAD. Now, I couldn't quite understand the second problem. Please explain a little better. PS: Now I know why I see all posts messed up.
Re: [Tutor] (no subject)
adam urbas escreveu: Hi,I just started python today and I would like a few pointers, if you don't mind. I tried using a tutorial, but was only able to get the correct results for the most basic problems. # Area calculation programprint “Welcome to the Area calculation program”print “–”print# Print out the menu:print “Please select a shape:”print “1 Rectangle”print “2 Circle”# Get the user’s choice:shape = input(“ “)# Calculate the area:if shape == 1:height = input(“Please enter the height: “)width = input(“Please enter the width: “)area = height*widthprint “The area is”, areaelse:radius = input(“Please enter the radius: “)area = 3.14*(radius**2)print “The area is”, areaI've been trying to get this to work. I was on a forum on Google and they said to put:input(press ENTER to continue)at the end. I did, but it didn't work. It runs the program but just shuts itself off when its done and i don't even get to select any of the option things that i'm s upposed to be able to select. It just turns on then back off and I don't even get to see anything. Could someone help me out.ThanksAdam _ Create the ultimate e-mail address book. Import your contacts to Windows Live Hotmail. www.windowslive-hotmail.com/learnmore/managemail2.html?locale=en-usocid=TXT_TAGLM_HMWL_reten_impcont_0507 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor First, welcome to the world of Python. :D Second. please give a title when you start a new thread on a mailing list. Third, format your posts and code. Since Python uses indented code, it's kinda hard to read it when it's all in one line (Don't worry, I'll paste it indented in a file attached to this email :D ) Now for the code. After arranging the code, the first thing I noticed were this characters “ ” I tried running the code, and if gave me a error there, so I just replace then with , and voilá, the code worked :D . So the lesson here is always use either or ' ' in the code. Oh, also another thing. Don't use input() to get the user input, because that command can run code and it may be evilly used. Always use raw_input() instead :D . Anyway, I hope I helped you, -- _ ASCII ribbon campaign ( ) - against HTML email X vCards / \ #!/usr/bin/python # Area calculation program print Welcome to the Area calculation program print âââââââââââââ print # Print out the menu: print Please select a shape: print 1 Rectangle print 2 Circle # Get the userâs choice: shape = input( ) # Calculate the area: if shape == 1: height = input(Please enter the height: ) width = input(Please enter the width: ) area = height*width print The area is, area else: radius = input(Please enter the radius: ) area = 3.14*(radius**2) print The area is, area ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor