880 Posted Topics
Re: [CODE]#python 3 def main(): i = int(input()) #input in python 3 return a string print(i + 1) #now it will add 1 to input #Call main() function main()[/CODE] | |
Re: [QUOTE]I'd like to know what it's used for. I think it's mostly used for web application's.[/QUOTE] Python is used in all application you can think off. Python is strong in web application's as you say with many good Web Frameworks. [url]http://wiki.python.org/moin/WebFrameworks[/url] Pyhon also have strong GUI-toolkit like wxpython,PyQT,PyGTK and bulid-in … | |
Re: Dont use [B]file[/B] as variable is a reserved keyword for python. So for this script under to work,[B]test.py[/B] an [B]words.txt[/B] need to be in same folder. [CODE]#test.py my_file = open('words.txt') #Remeber '' open take string as argument print my_file.read() my_file.close()[/CODE] Let say that [B]words.txt[/B] is not in same folder as … | |
Re: A good python IDE editor will help you more than notepad++. For windows [URL="http://code.google.com/p/pyscripter/"]pyscripter[/URL] or linux [URL="http://pythonide.blogspot.com/"]SPE[/URL] | |
Re: A parser like BeautifulSoup or lxml to exctract url. [QUOTE]url with some parameters.[/QUOTE] [url]http://docs.python.org/library/urlparse.html[/url] [url]http://atomized.org/2008/06/parsing-url-query-parameters-in-python/[/url] | |
Re: [url]http://stackoverflow.com/questions/3270209/how-do-i-make-tkinter-support-png-transparency[/url] Wxpython has good support for transparent image,and may be a better choice. For me there has never been a choice i find wxpython much better than Tkinter. The ugly look of Tkinter in windows is not ok at all. PyQt and pyGTK shold also has support for this. | |
Re: Why do you need to do system call with command line grep?,you can easily write this without it. Python has lot more power an dont need command line call like this to do this simple task. | |
Re: Yes Kur3k soultion is ok,a little long that last print line. [CODE]dic = { 1 : "Jan", 2 : "Feb", 3 : "Mar", 4 : "Apr", 5 : "May", 6 : "Jun", 7 : "Jul", 8 : "Aug", 9 : "Sep", 10 : "Oct", 11 : "Nov", 12 : "Dec" … | |
Re: Try with regex,and use an [ICODE]compile()[/ICODE] regex with [ICODE]finditer()[/ICODE] Look like this. [CODE]import re text = """\ Hi,i have car. I drive to work every day in my car. This text will find all car,car.car?!car.""" r = re.compile(r'\b(car)\b') for match in r.finditer(text): print match.group()[/CODE] | |
Re: [QUOTE]Currently, it worked fine with a dozen documents tested. But I think it's not robust enough. Do you know any library than can do the job?[/QUOTE]BeautifulSoup is very robust,not many parser are so good. You have [URL="http://lxml.de/"]lxml[/URL] that is good,it also has BeautifulSoup and html5lib build in. lxml has also … | |
Re: Regex an html are not best friend. Read the best answer out there(bobince) [url]http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags[/url] A parser is the right tool for this,here is an example with BeautifulSoup. [CODE]from BeautifulSoup import BeautifulSoup html = """ string_feature = '<div class="BVRRLabel BVRRRatingNormalLabel">Customer Rating</div><div class="BVRRLabel BVRRRatingNormalLabel">Value for Price</div> <div class="BVRRLabel BVRRRatingNormalLabel">Picture Quality</div> <div class="BVRRLabel … | |
Re: Use [ICODE]urlretrieve[/ICODE]. [CODE]>>> from urllib import urlretrieve >>> help(urlretrieve) Help on function urlretrieve in module urllib: urlretrieve(url, filename=None, reporthook=None, data=None)[/CODE] Example. [CODE]from urllib import urlretrieve urlretrieve('http://gdimitriou.eu/wp-content/uploads/2008/04/google-image-search.jpg', 'google-image-search.jpg')[/CODE] | |
Re: [QUOTE]Using tonyjv'advice i tried the fallowing with the code for the first function[/QUOTE] You are making some big mistake when you making it a fuction. You are putting in an argument to function that you not use inside the function [B]entry[/B] And you making a recursive call of the function … | |
Re: A couple way is split at / or use a regex. [CODE]>>> d = '1/2/2010'.split('/')[2] >>> d '2010' >>> import re >>> s = re.search(r'\d{4}', '1/2/2010') >>> s.group() '2010' >>> [/CODE] | |
Re: TreeCtrl is really not meant to interact with filesystem. wx.FileDialog is used to display files. | |
Re: The problem in python 3 code is that you are comparing integer with string. [ICODE]input()[/ICODE] in python 3 return a string same as [ICODE]raw_input()[/ICODE] in python 2. [ICODE]input()[/ICODE]in python 2 return an integer. This will work [CODE]answer = int(input("> ")) #make input return an integer[/CODE] Or you can cast it … | |
Re: Here is one way. [CODE]import re '''num.txt--> name2,number2;name3,number3;name1,number1 ''' with open('num.txt') as f: l = [i.strip().split(';') for i in f][0] print l #Test print def key_sort(string): result = re.split(r'(\d+)', string) for i in xrange(1, len(result), 2): result[i] = int(result[i]) return result l.sort(key=key_sort) print l """Output--> ['name2,number2', 'name3,number3', 'name1,number1'] ['name1,number1', 'name2,number2', … | |
Re: One with regex. [CODE]import re data = '''\ Correct for Detector Non-linearity: No (USB2E7196) Correct for Stray Light: No (USB2E7196) Number of Pixels in Processed Spectrum: 2048 >>>>>Begin Processed Spectral Data<<<<< 339.09 0.00 339.48 184.72 339.86 186.46 340.24 187.76 340.63 189.11 341.01 190.97 ... ... 1023.36 196.86 1023.65 196.36 >>>>>End … | |
Re: Look at cgi module. [url]http://docs.python.org/library/cgi.html[/url] [url]http://www.tutorialspoint.com/python/python_cgi_programming.htm[/url] Most common way to work with HTML/websites is to use one of python Web framework. [url]http://wiki.python.org/moin/WebFrameworks[/url] | |
Re: You can take a look at this code. Python also have a [URL="http://docs.python.org/library/csv.html"]CSV module[/URL] [CODE] my_list = [] with open('my_csv.txt') as f: l = [i.strip().split(',') for i in f] for i in range(len(l)): my_list.append([int(n) for n in l[i]]) print my_list print my_list[0][1] #Take out a number print my_list[-1] #Take out … | |
Re: Look into regular expression for changing filename. Here is an example. [CODE]import re files = '''\ lovelyname_annoying_chars.txt Diename_annoying_charshard.txt somenamename_annoying_chars.txt''' new_files = re.sub(r'name_annoying_chars', '', files) print new_files """Output--> lovely.txt Diehard.txt somename.txt """[/CODE] | |
Re: Python string are immutable so delete it wont work. You can replace it first character with nothing [B]''[/B]. [CODE]>>> s = "this is a really long string that is completely pointless. But hey who cares!" >>> s.replace(s[0], '') 'his is a really long sring ha is compleely poinless. Bu hey … | |
Re: Look at the output -ordi- [B]fiss gisse hd uc tb oa z[/B] Do you see some character are missing? I dont know how BirdaoGwra want the output,here are a couple of way. fissgiss is one word in this,some more work is neede to split that to. Pickle is way to … | |
Re: [B]read()[/B] read the whole file into a string,so no need [B]for lines in data[/B] [B]with open([/B]) close the file object auto. So you can write it like this. [CODE]import urllib data = urllib.urlopen('http://www.site.net/index.html').read() with open('somefile.html', 'w') as f: f.write(data) [/CODE] | |
Re: Something you can look at,for doing financial calculations look at Decimal module. [url]http://docs.python.org/library/decimal.html[/url] [CODE]>>> number = 0.60 >>> print '%.2f' % number 0.60 >>> from decimal import * >>> Decimal('0.60') Decimal('0.60') >>> Decimal('0.60') + Decimal('0.60') Decimal('1.20') >>> >>> print Decimal('0.60') + Decimal('0.60') 1.20 [/CODE] | |
Re: Fix your indentation is wrong in many places. This has to be right in python. | |
Re: [QUOTE]thanks. What are major differences using Iron Python vs Python 2.6.6? [/QUOTE] With ironpython you can use the .Net(framework)library. Same as Jython there you can use java library. You write in python,and import from Net library. [QUOTE]IronPython is an implementation of the Python programming language running under .NET and Silverlight. … | |
Re: A couple of way to make it a list of integer and sort. First one use list comprehension. Second one use a function programming style with map(). [CODE]>>> print sorted([int(i) for i in a]) [2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, … | |
Re: [QUOTE]1) Is there any way to split ['a', 'b c'] into ['a', 'b', 'c']?[/QUOTE] [CODE]>>> l[0].split() + l[1].split() ['a', 'b', 'c'][/CODE] As postet over,repost your code with code tag. And include the error message you get called(Traceback) | |
Re: [QUOTE]I have been told by a number of people to use xpath instead of regex for some of my regex searches.[/QUOTE] What are you searching? Some notes about XML/webpages here is regex not the right tool. If you are parsing(searching) XML/html you should use beautifulsoup / lxml(has XPath built in) … | |
Re: Dident get you pysnmp script to work. Here is a working py2exe kode. [CODE]from distutils.core import setup import py2exe import sys def py_exe(file_in=None): if len(sys.argv) == 1: sys.argv.append('py2exe') setup(options = {'py2exe': {'compressed': 1, 'optimize': 2, 'ascii': 1, 'bundle_files': 3}}, zipfile = None, ## Can use console or window ## Filpath … | |
Re: Can you post the script,so shall i test it in py2exe and cxfreeze. | |
Re: You should(most) read some tutorials/book about using classes. The way you are doing is not god at all and wrong. Like basic thing that a class need the [B]self[/B] keyword. Can write an example that makes more sense. [CODE]class numbers(object): def addition(self,a,b): self.a = a self.b = b return self.a … | |
Re: [CODE]import re text = '''\ #include "hello.h" #include "d/hello.h" #include "dir/hello.h" #include "dir\hello.h" #include <hello.h> #include "a\b\c.h" #include <ref\six\eight.h> #include "123\456/789.h" #include "bye.h" ''' new_text = re.findall(r'\w+\.\w' ,text) print new_text #list print '\n'.join(new_text) #string print set(new_text) #No duplicate """Output--> ['hello.h', 'hello.h', 'hello.h', 'hello.h', 'hello.h', 'c.h', 'eight.h', '789.h', 'bye.h'] hello.h hello.h … | |
Re: What is the full name,and what should the new full name be?,you most explain better. IMG.jpg to HH.jpg? HH1.jpg HH2.jpg....? | |
Re: [CODE]>>> l = ['asd',"asd'das",'qw','213'] >>> l ['asd', "asd'das", 'qw', '213'] >>> l.pop(0) 'asd' >>> l ["asd'das", 'qw', '213'] >>> l.pop(1) 'qw' >>> l ["asd'das", '213'] >>> [/CODE] This is what`s happens. [B]l.pop(0)[/B] take out take out [B]'asd'[/B] Then [B]"asd'das"[/B] take place as element [B]0[/B] Next when you use [B]l.pop(1)[/B] it … | |
Re: [QUOTE]humansize.py from the >>> prompt in the shell (interactive IDLE). How do I do that?[/QUOTE] You dont do that because it wrong. To run a script. From [B]cmd[/B] [ICODE]python humansize.py[/ICODE] or as posted by tony by using [ICODE]File->new window then F5(run)[/ICODE]. If you want to use humansize.py in IDLE you … | |
Re: You can use [URL="http://docs.python.org/library/pickle.html"]Pickle[/URL] [CODE]import pickle import numpy X = numpy.matrix(numpy.zeros([801,801])) print 'Before pickle' print '-'*30 print X print '-'*30 def write(data, outfile): f = open(outfile, "w+b") pickle.dump(data, f) f.close() def read(filename): f = open(filename) data = pickle.load(f) f.close() return data if __name__ == "__main__": some_data = X write(some_data, "temp.file") … | |
Re: You are looking for random.shuffle(). To see all method under random module use [B]dir(random)[/B] For help use [B]help(random.shuffle)[/B] [CODE]>>> random_name = ['Mohamed', 'Ahmed','Aboubakr'] >>> random.shuffle(random_name) >>> random_name ['Mohamed', 'Aboubakr', 'Ahmed'] >>> random.shuffle(random_name) >>> random_name ['Ahmed', 'Mohamed', 'Aboubakr'] >>> random.shuffle(random_name) >>> random_name ['Ahmed', 'Aboubakr', 'Mohamed'] >>> [/CODE] | |
Re: A little help. [CODE]>>> import string >>> string.ascii_letters 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' >>> string.punctuation '!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~' >>> [/CODE] In this code i exclude [B]string.ascii_letters[/B] [CODE]import string exclude = string.ascii_letters s = 'test.. hi? for ,,,' punc_text = ''.join(ch for ch in s if ch not in exclude) print punc_text #.. ? ,,,[/CODE] [QUOTE]I keep … | |
Re: [QUOTE]Any idea how to change the format to something like this?[/QUOTE] You can use the new string formatting has a lot of powerful features an was new in python 2.6 [url]http://docs.python.org/library/string.html#formatstrings[/url] An example. [CODE]google = { 'facebook.com': 230, 'yahoo.com': 9, 'fifa.org': 67, 'msn.com': 3} print 'google.com' for name in sorted(google): … | |
Re: Try this. [CODE]import urllib.request page = urllib.request.urlopen("http://www.randompickupline.com/") text = page.read().decode("utf8") where = text.find('<p id="pickupline">') start_of_line = where + 19 end_of_line = start_of_line + 150 line = (text[start_of_line:end_of_line]) pick_text = line.find('</p>') print (line[:pick_text]) """Out--> Do I know you? (No.) That's a shame, I'd sure like to. """ [/CODE] When it comes … | |
Re: A print statement should clear it up. Here first line get run,where user input return an integer. Then that integer is passed inn as an agument to function adder. Then d take that value and put result in a new variable d. Then we print out result. Because of bad … | |
Re: [QUOTE]i agree with the splicing which i've found helpful. [/QUOTE] "splicing" is that a secret python slicing method,just kidding :icon_wink: "splicing" in action. [CODE]>>> s = 'hello' >>> s[::-1][-1] 'h' >>>[/CODE] | |
Re: Log in that use Javascript make the process a lot harder. Read [URL="http://wwwsearch.sourceforge.net/old/bits/GeneralFAQ.html"]Embedded script is messing up my web-scraping[/URL] You have [URL="https://github.com/davisp/python-spidermonkey"]Spidermonkey[/URL] [QUOTE]"Execute arbitrary JavaScript code from Python. Allows you to reference arbitrary Python objects and functions in the JavaScript VM"[/QUOTE] Look at [URL="http://pypi.python.org/pypi"]PyPi[/URL] for tool that can help. | |
Re: If you want to move an item that's already in the list to the specified position, you would have to delete it and insert it at the new position. [B]l.insert(newindex, l.pop(oldindex))[/B] [CODE]>>> l = [[5,5,5],[5,5,5,5,5],[]] >>> l.insert(1, l.pop(2)) >>> l [[5, 5, 5], [], [5, 5, 5, 5, 5]] >>> … | |
Re: Rember use code tag. [CODE]data1 = [] x_org = [] locx = open('test.txt', 'w') for i in range(9): x_org.append(i) locx.write('\n'.join([str(i) for i in x_org])) locx.close()[/CODE] Can break it up it little so it`s easier to understand. [CODE]>>> l = [1, 2, 3, 4, 5, 6, 7, 8] >>> #We have … | |
Re: [QUOTE]but i m not getting output...wen i run a program it is showing error .[/QUOTE] Then you have to post the error we are not mind reader. The code from -ordi- works fine,and [B]with[/B] is a good thing to use because it close() file auto. If you use python 3 … | |
Re: As first line use this and it will work. [B]from __future__ import division[/B] Or. [CODE]TF = float(raw_input("Enter a Farenheit temp: ")) TC = (5/9.0)*(TF-32) #9.0 make it float [/CODE] Read this. [url]http://www.ferg.org/projects/python_gotchas.html#contents_item_3[/url] |
The End.