880 Posted Topics
Re: Read this. [url]http://stackoverflow.com/questions/1732348/regex-match-open-tags-except-xhtml-self-contained-tags[/url] So regex it not the right tool when it comes to html/xml. There is a reason why parser excit,python has 2 very good lxml and BeautifulSoup. [CODE]from BeautifulSoup import BeautifulSoup html = """\ <div class="test" src="http://www.test.com/file.ext" style="top:0px;width:100%;>" """ soup = BeautifulSoup(html) tag = soup.find('div') print tag['src'] #--> … | |
![]() | Re: Pyhon has Tkinter,wxpython,PyQt(pyside),PyGTK that you can make GUI with. Also ironpython,jython can be used to make GUI. You say window form builder it is used by Visual Studios to make GUI as mention by Theh B. Just same can you do with GUI-toolkit mention above. And better those GUI-toolkit are … |
Re: Like this with correct indentations. [QUOTE]I double checked my indentation and this still doesn't work for me,[/QUOTE] Do not post that code dont work,python always give you [B]Traceback[/B]. Next time post code and Traceback,then is`t much eaiser to help. Understanding Traceback is very important part of programming in python. 1 … | |
Re: Something like this,if you want sting 1,string 2.... just iterate over the content. [CODE]from BeautifulSoup import BeautifulSoup html = '''\ <tr id="index_table_12345" class="index_table_in"> <td><a href="/info/12345">string 1</a></td> <td><a href="/info/12345">string 2</a></td> <td><a href="/info/12345">string 3</a></td> <td><a href="/info/12345">string 4</a></td> <!--td></td--></tr>''' soup = BeautifulSoup(html) tag = soup.findAll('td') #all "td" tag in a list tag_a = … | |
Re: For python 2 and 3,i think you use pyhon 3 because you get error on print. [CODE] #Python 2.7 >>> print r"pow(2,3) returns 2^3 pow(2,3,1) returns 2^3 modulo 1", pow(2,3), pow(2,3,1) pow(2,3) returns 2^3 pow(2,3,1) returns 2^3 modulo 1 8 0[/CODE] [CODE] #Python 3 >>> print(r"pow(2,3) returns 2^3 pow(2,3,1) returns … | |
Re: Tk GUIs don't look and behave like a native application, which affects the "look & feel" in multiple small ways, which make a complex app feel weird and clunky compared to native apps on given system. So Tkinter will not look god on windows. For more better looking GUI look … | |
Re: There are many postet how to use py2exe and cx_freeze on this site. Just search. | |
Re: [CODE]infile = open("afile.txt") count = {} for aline in infile: words = aline.split() for word in words: if word in count: count[word] += 1 else: count[word] = 1 print(count)[/CODE] An other way this code also remove punctuation and covert to lower case. [CODE]from collections import Counter with open('afile.txt') as f: … | |
Re: Dont use input as a varible name,is reserved word for user input. To simpilfy it,there is [ICODE]capitalize[/ICODE] string method. [CODE]>>> s = 'the money is in the bag. however you dont want it.' >>> [i.capitalize() for i in s.split()] ['The', 'Money', 'Is', 'In', 'The', 'Bag.', 'However', 'You', 'Dont', 'Want', 'It.'][/CODE] … | |
Re: Some code that should help,it is close to what pyTony suggests. Making a dictionary will be suited for this. [CODE]with open('fruit.txt') as f: l = [i.strip() for i in f] my_dict = dict(zip(l[0::2], l[1::2]))[/CODE] Test in IDLE. [CODE]>>> my_dict {'apple': '1.99', 'coconut': '1.59', 'orange': '9.99'} >>> search = 'apple' >>> … | |
Re: This will do it. [CODE]with open('file_in.txt') as f: for numb,line in enumerate(f, 1): if 1 <= numb <= 150: print line[/CODE] | |
Re: You can look at this and see if it helps. [CODE]>>> import socket >>> socket.gethostbyname_ex('python.org') ('python.org', [], ['82.94.164.162']) >>> socket.gethostbyaddr('82.94.164.162') ('dinsdale.python.org', [], ['82.94.164.162']) >>> socket.gethostbyaddr('111.111') Traceback (most recent call last): File "<interactive input>", line 1, in <module> gaierror: [Errno 11004] getaddrinfo failed >>> socket.gethostbyaddr('11.111.111.111') Traceback (most recent call last): File … | |
Re: [QUOTE]I want to extract the following link "http://media1.santabanta.com/full5/indian celebrities(f)/aalesha/aalesha-1a.jpg".[/QUOTE] There is a problem the link you want is loaded bye javascript. We can see the link in downloaded text,then we can drop to simulate javascript and use regex(because Beautifulsoup cant find stuff in javascript) [CODE]from urllib2 import urlopen from BeautifulSoup … | |
Re: Windows look in [ICODE]environment variable(path)[/ICODE] for python version. [url]http://www.windows7hacker.com/index.php/2010/05/how-to-addedit-environment-variables-in-windows-7/[/url] To path you can [ICODE]add[/ICODE] [B];C:\python27\;C:\python27\scripts[/B] Restart. When you write pyhon in cmd python 2.7 will start. Change to [B];C:\python32\;C:\python32\scripts[/B] Restart. When you write pyhon in cmd python 3.2 will start. This also mean that stuff get installed in python 3.2. … | |
Re: An other way,nice use of itertools Gribouillis. [CODE]def blocks(filename, chunks): with open(filename) as f: numb_list = [i for i in f] return [numb_list[i:i+chunks] for i in range(0, len(numb_list), chunks)] filename = 'numb.txt' chunks = 4 print blocks(filename, chunks) """ Out--> [['56.71739\n', '56.67950\n', '56.65762\n', '56.63320\n'], ['56.61648\n', '56.60323\n', '56.63215\n', '56.74365\n'], ['56.98378\n', '57.34681\n', … | |
Re: All makeMagnitudeList() function do is to return maglist. outfile will be local to function,to make it work we have to move it out. Remember to [ICODE]close()[/ICODE] file or use [ICODE]with open()[/ICODE]. you can not write a list to file,it has to string. If you give a sample of earthquakes.txt file … | |
Re: Some note. For better look/style than just stuff all code in a main function,it can be better to make som more functions. Then bring it all togeher in the main function. Here you also see that i use [ICODE]docstring[/ICODE] in function name_calc. [CODE]def name_input(): name = raw_input("Please enter your first … | |
Re: If .py files are assigned to be opened by python on your system, then you only need to put a shortcut into your startup folder. C:\Documents and Settings\All Users\Start Menu\Programs\Startup More andvance solutions is. - add it to the windows registry (HKCU\Software\Microsoft\Windows\CurrentVersion\Run) - package it into a service, that should … | |
Re: Some point to this. [CODE]>>> a = 2 >>> b = 1 >>> c = 4 >>> d = 3 >>> l = [a,b,c,d] >>> l [2, 1, 4, 3] >>> sum(l) 10 >>>[/CODE] So here i declare variables an put it in a list and [ICODE]sum()[/ICODE] it up. [ICODE]['a','b','c','d'][/ICODE] … | |
Re: You have to explain yourself better. Just one way to take out a line. [CODE]data = '''\ 450 42.5 16625.0 460 42.0 16820.0 470 41.5 17005.0 480 41.0 17180.0 490 40.5 17345.0 500 40.0 17500.0''' for line in data.split('\n'): if line.startswith('470'): print line #470 41.5 17005.0[/CODE] | |
Re: [CODE]>>> equation = 5 >>> if equation == a: ... print 'something' ... Traceback (most recent call last): File "<interactive input>", line 1, in <module> NameError: name 'a' is not defined >>> a = 5 >>> if equation == a: ... print 'something' ... something >>> [/CODE] Python always read … | |
Re: I think you use python 3? In python 3 input() return a string. And formula will not work. You can try this,and use code-tag also post error message(Traceback) [CODE]import cmath print("Welcome to the Quadratic Program") print("--------------------------------") a = int(input("Enter a: ")) b = int(input("Enter b: ")) c = int(input("Enter c: … | |
Re: [CODE]import os with open('file_1.txt') as file_1, open('file_2.txt') as file_2: f1 = [i.strip() for i in file_1] f2 = [i.strip() for i in file_2] comp = [f for f in f2 if f not in f1] for f in comp: #print f #print os.path.basename(f) with open('new.txt', 'a') as f_out: f_out.write(f + … | |
Re: As pyTony say you complicated it by use of ternary operator([ICODE]a if b else c[/ICODE]) One thing also to remember is to remove punctuation. Here one with translate. [CODE]from string import punctuation def is_palindrom(word): word = word.lower().translate(None, punctuation) return word == word[::-1] print is_palindrom('Saippuakivikauppias') #True[/CODE] And a test with a … | |
![]() | Re: Here something you can look at. [CODE]>>> s = 'The quick brown fox jumps over the lazy dog' >>> [i for i in s.split() if 'e' not in i] ['quick', 'brown', 'fox', 'jumps', 'lazy', 'dog'] [/CODE] Can break it up,code over is list comprehensions wish is much used in python. … |
Re: There are many years since DOS was a part of windows. No there is [B]cmd[/B](command line),but that is not DOS. They only way run DOS these day,is to run it as an own OS. | |
Re: [QUOTE]And just an FYI: The Professor said the code could be done in very few lines (15 or less) so keep that in mind.[/QUOTE] He was nice with 15 lines you can du a lot in python. [CODE]>>> ''.join(i + '*' for i in raw_input('word: ')) word: Hello 'H*e*l*l*o*' >>> … | |
Re: Uinicode should work regardless of the encoding used. [CODE]#u.py print unichr(163) print u"\u00A3" Microsoft Windows [Versjon 6.1.7601] Copyright (c) 2009 Microsoft Corporation. Med enerett. C:\>python u.py £ £[/CODE] | |
Re: Just a note var2=[] is not needed as findall() is returing a list. [ICODE]finditer()[/ICODE] can also be a good solution for iterating over larger files. Always use raw string with regex [ICODE]r' '[/ICODE] [CODE]import re data = '''\ This rule says that any match that begins earlier in the string … | |
Re: When you use 's' it`s a string s,not variable s. [CODE]>>> int('s') Traceback (most recent call last): File "<interactive input>", line 1, in <module> ValueError: invalid literal for int() with base 10: 's' >>> s = '42' >>> type(s) <type 'str'> >>> x = int(s) >>> x 42 >>> type(x) … | |
Re: When you get IOError,pass make it countinue to end. Other errors than IOError will raise error as normal. [CODE]try: doSomething() except IOError: pass[/CODE] | |
Re: [QUOTE]so why doesn't it (the code above) work?[/QUOTE] Why shoud it? Break it thing down to understand what happends. [code=python] >>> class my_class: def __init__ (self): self.name = ' my name is hi' >>> a = my_class() #Class instance >>> print a <__main__.my_class instance at 0x011E23F0> >>> # This only … | |
Re: You are making some mistake and it can be done much eaiser with some python power. You are trying to count with dictionary 2 times 1 is enough. [B].strip(',:.;')[/B] You should take out more than this also ?! An example. [CODE]>>> import string >>> string.punctuation '!"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~' >>> s = 'The … | |
Re: Remember use code tags. Here a some lines that should help you. [CODE]>>> s = 'John Marvin Zelle' >>> l = s.split() >>> l = ''.join(l) >>> l 'JohnMarvinZelle' >>> sum((ord(i)-64 for i in list(l.upper()))) 184[/CODE] And dont use sum as variable name,as you se in code over sum() is … | |
Re: I can help you with first problem. You have to show some effort,some just post there school task here. That means that you have to post some code of what you are struggling with. [CODE]>>> s = 'a fine day with beautiful sun' >>> l = s.split() >>> l ['a', … | |
Re: [QUOTE]P.S on what highest python version wxpython works?[/QUOTE] 2.7 [URL="http://wiki.wxpython.org/TentativeRoadmap"]wxpython Roadmap[/URL] Just a note dont use findall() in a loop. findall() iterate through all text it`s given. [CODE]g = regex.findall(regex, failas.read())[/CODE] If you shall iterate through to text,use finditer() [CODE]for line in failas: for match in regex.finditer(line): print (match.group())[:-1][/CODE] And … | |
Re: [QUOTE]However when I run the program it ends up blue just like the "please enter our name" string.[/QUOTE] Blue is correct,red comes if you make an error. This is how it should be and has always been in IDLE. You can try to change that color option->configure IDLE. But that … | |
Re: You can also use cryptographic solution,python has a good libary build in [ICODE]hashlib[/ICODE]. Here is a quick demo with [URL="http://en.wikipedia.org/wiki/SHA-2"]sha512[/URL] that is very safe. [CODE]>>> import hashlib >>> hashlib.sha512("my password").hexdigest() 'e28bdbf8faa97dab2203fcc89e397a4bf8d4a5b370421e5481a55f317caee4f81be5a810bb1cffc4695c32198717b9a6e835895852ee3a8689d0963463f2db15'[/CODE] Here we get 128 character,this is a one way solution. So how to now that this character 128 is … | |
Re: You may have to give more exact info,like post more of the file. Here is a regex example with the string you posted,that may need some changes to be more greedy. [CODE]import re s = '(a,b){var c=encodeURIComponent,d=["//www.google.com/gen_204?atyp=i&zx=",(new Da "' r = re.findall(r'\gen_(\d.*)="', s) print(r) #['204?atyp=i&zx'][/CODE] | |
Re: [CODE]>>> s = [('word1', 3), ('word2', 2), ('word3', 1), ('word4', 1), ('word5', 2)] >>> for i in s: ... print '%s %s' % (i[0], i[1]) ... word1 3 word2 2 word3 1 word4 1 word5 2 >>> [/CODE] If writing to a file. [CODE]s = [('word1', 3), ('word2', 2), ('word3', … | |
Re: Here is a proxy test that worked for me. Maybe it can help you. [CODE]import urllib2 proxy_handler = urllib2.ProxyHandler({'http':'184.73.131.27:3128'}) opener = urllib2.build_opener(proxy_handler) f = opener.open('http://www.whatismyipaddress.com') print f.read()[/CODE] | |
Re: [QUOTE](I dont even know what traceback means[/QUOTE] Traceback you get when something goes wrong. [CODE]>>> a Traceback (most recent call last): File "<interactive input>", line 1, in <module> NameError: name 'a' is not defined >>> # We most define a >>> a = 5 >>> a + '6' Traceback (most … | |
Re: [QUOTE]But still nothing is working for webbrowser.get('whateverbrowser')[/QUOTE] webbrowser.get('whateverbrowser') will search in [URL="http://windows7themes.net/how-to-change-environment-variables-in-windows-7.html"]environment variable[/URL] path. If it dont find path to firefox it will give back. webbrowser.Error: could not locate runnable browser So to environment variable [B]path[/B] you have to add path to firefox. [B];c:\...path to firefox[/B] for me it look … | |
Re: [QUOTE]i'm thinking maybe it's an indentation problem but I don't know [/QUOTE] [ICODE]while if elif else while if elif continue if .....[/ICODE]a mess where am i now:confused: Most can understand a while if elif else..,but if you nest it together then code lose readability. The code will be much harder … | |
Re: [ICODE]chr[seq[x]][/ICODE] wrong chr[] shall be chr(). But that dosent matter it will still be wrong. You most split user input,so if someone enter 67 65 82 they get CAR. [CODE]seq = input("enter string ASCII numbers to be converted into text: ") text = '' for item in seq.split(): text += … | |
Re: As Tony poster regex is not a god choice for html or xml. This is why paser exist to do this job. [CODE]from BeautifulSoup import BeautifulSoup html = '''\ <html> ...some code.... <B><FONT color="green">TEXT to BE EXTRACTED 1</FONT></B><br> <P> <B><FONT color="green">TEXT to BE EXTRACTED 2</FONT></B><br> <P> <B><FONT color="red">TEXT to BE … | |
Re: [CODE]>>> knights = {'gallahad': 'the pure', 'robin': 'the brave'} >>> for k, v in knights.iteritems(): ... print k, v ... gallahad the pure robin the brave[/CODE] If you use python 3,then print is a function. So it have to look like this. [ICODE]print(k, v)[/ICODE] and [ICODE]iteritems()[/ICODE] is [ICODE]items()[/ICODE] [CODE]# Gribouillis … | |
Re: CGI scripting is not used so much. Most of web development in python done with web framework. [url]http://wiki.python.org/moin/WebFrameworks[/url] There are many good like web2py,Django,TurboGear,Pylons. Django is the most know. Some web site made in django. [url]http://www.djangosites.org/[/url] | |
Re: Enalicho has give some tip. Here a couple more. Always use [ICODE]r(raw_string)[/ICODE] for regex expression. If you want to count,just use [ICODE]len()[/ICODE] because [ICODE]re.findall()[/ICODE] is returning a list. Here is a example run. [CODE]>>> import re >>> user_input = 'aaaaBBBCC' >>> re.findall(r'A-Z', user_input) [] >>> #Missing [] >>> re.findall(r'[A-Z]', user_input) … |
The End.