133 Posted Topics
Re: Read the last post of masterofpuppets to your other thread ([url]http://www.daniweb.com/forums/thread235765.html[/url]) and you'll nearly have the answer... Put his code in a function (pascrow), change the for loop condition and the print statement and you've got it | |
Re: Oh yes, you've got another choice. Use your brain. This assignment is fairly doable and, if you search this forum or others, you'll find plenty of ways to do it. I'm afraid that your post shows someone that doesn't want to bother with this assignment. I'd be interested in knowing … | |
Re: doing x=Stuff make of x an alias to the Stuff class. Try this : [CODE] class Test: def __init__(self): self.a="a" x=Test print x y=x() print y.a [/CODE] This may be useful if you want to pass an object definition as an argument for a function. Never happened to me yet … | |
![]() | Re: Totally agree with Gribouillis. This is not a good way of doing things. Many problems may occur. You may use a dictionnary instead: [CODE] def myfunc(arg1, arg2): d={'x':2, 'y':3, 'z':4} return d[arg1], d[arg2] print myfunc('x', 'y') # returns: 2, 3 print myfunc('y', 'z') # returns: 3, 4 [/CODE] But in … |
Re: you can use list comprehensions [CODE] g = "ThisIsATestt" print [g[i:i+2] for i in range(0,len(g),2)] [/CODE] | |
Re: My 2 cents... 1 - I prefer for loop than while ones when I can (less code to write and no infinite loops). [CODE] start = int(raw_input("Start at: ")) end = int(raw_input("End at: ")) count = int(raw_input("Count by: ")) for i in range(start,end+1,count) : # end + 1 because range … | |
Re: I would add that [B]exactly[/B] the same question was posted 2 days ago. [url]http://www.daniweb.com/forums/thread230626.html[/url] It would be a good idea to search the forum before asking... | |
Re: It points to the original variable. This is true for all variables containing objects. For a list, if you want to duplicate it, you have to do [CODE] mylist = [1,2,3,4] a = mylist[:] a[0] = 'hello' print mylist [/CODE] | |
Re: first of all your function getdata should return the values you want : [code] def getdata(data): # the code you've already written return number, items [/code] | |
Re: [CODE]numQuiz = input("Enter the number of quizzes: ") total = 0 for i in range(numQuiz): print ('Enter quiz score %d: ' % i) # # OR print ('Enter quiz score ', str(i), ' : ') score= input() total = total + score print "------------------------------------" # Compute the average and print … | |
Re: Beware of the ''. You should write [CODE] textfile = file('C:\\HourlyATC\\ATC101509\\10-15-09 to 10-21-09 ATC.txt','w') # \\ and 'w' [/CODE] | |
Re: Could you post some code or be more precise ? Is this list made of objects from the same class ? Maybe should you implement the __cmp__ OR __eq__, __ge__, __gt__, __le__, __lt__ and __ne__ special method in your object... That would allow comparisons between objects. and sort them | |
Re: Why do you want to have vars named POSn. I wouldn't do that... Very complicated. It's far easier to have one list variable where you assign pos[n] [code] pos=[' ' for i in range(10)] # defines a list inited with [' ', ' ',... ten times] pos[POS-1]='->' [/code] | |
Re: The question is : what error do you have ? | |
Re: I don't think so : zip.write waits for a file NAME and you pass a file OBJECT. I think this may be done by using the second argument of zip.write : [CODE] zip.write(filename, arcname=None, compress_type=None) # filename = the file you want to archive # arcname the name you give … | |
Re: I don't have time to test extensively your code but i'd begin by looking at loops. I've seen a couple of while True... How long do you stay in these loops ? You should test and locate precisely where your program lose time. | |
Re: I can't copy the text to test... Is this to be a html file ? Are the <br /> tags or are they supposed to show CR. Your BBCodes don't work... How can you select one line if all your file seems to be on an unique line ? | |
Re: So please, go on, repost your code with appropriate tags around it | |
Re: a fourth : [CODE] import re st = "abcd123" rdigit=re.compile("\d") print rdigit.findall(st) [/CODE] Which can be onelined : [CODE] import re print re.compile("\d").findall("abcd123") [/CODE] | |
Re: [CODE] outfile = 'OUTPUT' output = open(outfile, 'w') for line in open('DATA'): # You don't need to open the file and load it in a list (bad for memory) linelist = line.strip().split() # Strip will remove trailing spaces and '\n' if linelist[1][4:6]=='MG' and linelist[1][-2:] not in ['RE', 'RD']: output.write(line) [/CODE] | |
Re: I think this one is better... [CODE] import re testStrings=[ "POST at the start without the other word", "GET at the start without the other word", "GET first\nthen POST and more", "POST first\nthen GET and more"] splitter = re.compile('(POST|GET)') for s in testStrings: splittedString=splitter.split(s)[ 1:] print [ splittedString[i]+splittedString[ i+1] for … | |
Re: [ ] is generaly used for indexing (see the list, tuple or dictionary chapters in the doc). [CODE] mylist=[ 1,2,3,4,5,6] print mylist[ 3] > 4 [/CODE] If you see strings like some kind of char tuple (immutable list), everything appears to be very simple as it works exactly the same. … | |
Re: First of all, rewrite your code with correct indent. This is very significant in python and your code is very strangely indented (i'm surprised this works...) class effectIllusion shouldn't be indented, nor def updateSimObjects(self): if not mount0: and following is inconsistent... | |
Re: and you should use float instead of int [CODE]print float(miles_driven) / float(gallons_of_gas_used) [/CODE] | |
Re: [QUOTE=woooee;1002040] [CODE] ## and if you have an unusual series of numbers, use a list iterate_list = [0.0, 0.2, 0.3, 0.5, 0.6] for r in iterate_list: print r[/CODE][/QUOTE] To continue in this direction, you can use a list you construct the same time (called list comprehension)... This is not very … | |
Re: Vegaseat forgot a little point [QUOTE=vegaseat;1001729] [code=python]# This script will determine the size if the gwarchive directories # in everyone's home folder import os folder_size = 0 for username in open("L:\\dirl.txt"): folder = "L:\\" + username.strip('\n') + "\\gwarchiv" # <== remove the ending "\n" print folder for (path, dirs, files) … | |
Re: Beware of your indent... Very important in python [code] print 'one' print 'two' # <== ERROR (one space too much) print 'three' if condition # < ERROR (missing : ) print "four" # < ERROR (missing indent) [/code] | |
Re: [QUOTE=baki100;1000295]you can use a dictionary but it won't be wise to use the surname as the key instead use the position number. for the itemvalues you can use a list and you can append new values later if needed so basically you have a list in the dictionary.[/QUOTE] It is … | |
Re: [QUOTE=chico2009;999542]Hi Thanks folks for your replies. Testing the general format shown below returns a character and not the complete line [code=python] lpa=1 text_file = open("English101.txt", "r") print text_file.readline()[lpa] text_file.close() [/code] What I am trying to achieve is that a complete line of text is returned, from a text file that … | |
Re: Could you post an extract of the input file and an example of what you would like in the output one... | |
Re: [QUOTE=gerard4143;995454]Why don't you def your function like [CODE] def second(s): str = string.split(s) return str [/CODE] then you can print it like print str(second(string))[/QUOTE] I don't understand the problem : if you do like this, you can call the function from the other file doing : print str(twond.second(string)) and it … | |
Re: I'd store the blocks in a list, looking for the pattern in each line and, if i find the pattern in one line, write the whole block out. [CODE] isBlockToWrite=False pattern="COMMS_MESSAGE_SENT" block=[] outfile=file("log3.log","w") for line in file("log2.log"): block.append(line) if line[:10] == "*" * 10: if isBlockToWrite: outfile.writelines(block) block=[] isBlockToWrite=False if … | |
Re: what is the precise error message ? How did you install python ? What is your OS ? | |
Re: this may be working. [CODE] import re datas=open("file.html").read() expr=re.compile("<adress>(.*?)</adress>") for match in expr.findall(datas): print match [/CODE] | |
Re: Here are somme comments on what you ask. This is not a working program but gives you functions you can use for the different steps using your way. I don't understand everything you are doing and this seems very complicated to me. Very old style to deal with files. Python … | |
Re: I post my answer here too (i did it first on your other thread) you've got an easy way of doing this : [code] datas="""[(0,1),(1,1),(2,1),(2,2),(1,2),(0,3),(-1,4),(-2,4),(-1,5)] [(0,1),(1,1),(2,1),(2,2),(1,2),(0,3),(-1,4),(-2,4),(-1,5)] [(0,1),(1,1),(2,1),(2,2),(1,2),(0,3),(-1,4),(-2,4),(-1,5)]""".split('\n') for data in datas: exec ("lst=%s" % data) Prot='HPPHPPHHP' for i, coord in enumerate(lst): for elt in coord: print elt, print Prot[i] [/code] … | |
| |
Re: [QUOTE=AutoPython;988381] [CODE] def testfunc(): global c c = "test" testfunc() c1 = c del c print (c1) input () [/CODE] [/QUOTE] My question is : why don't you do [CODE] def testfunc(): return "test" c1 = testfunc() print (c1) [/CODE] I totally agree with what is said : globals should … | |
Re: Just to help you to understand what you have to do : a is a power of b if it is divisible by b and a/b is a power of b : a is power b => a/b is power of b : example : 1000 is power of 10 … | |
Re: I don't know about this tool but i would guess that the script tries to delete an opened file (is it a zip file or a file inside a zip file, i don't know) | |
Re: You can try this : [code] fileName = raw_input("Enter the file name you want to read (name.txt): ") comments=[] inComment=False for i, line in enumerate(open(filename)): # You can loop directly on the file lines if "//***" in line: inComment = not inComment if inComment: print "comment found at line %d" … | |
Re: [QUOTE=sravan953;986688] Also, for the GUI, you have tkinter and wxPython. tkinter is a little old, so wxPython is the order of the day! ;)[/QUOTE] But tkInter is included in the standard distribution... ![]() | |
Re: This may help you : [url]http://sourceforge.net/projects/mysql-python/forums/forum/70460/topic/2316047[/url] | |
Re: You should not remove elements from a list you are looping on [code] list=[1,2,3,4,5,6,7,8,9] for i in list: if i in [4,5,6]: list.remove(i) print list > [1, 2, 3, 5, 7, 8, 9] [/code] When you remove an element, the followings ranks are modified (minus 1) so some elements are … | |
Re: Here is a solution... [code] datas="""2 1 863.8 300.2 0.0131 0.0759 0.1727 0.0879 1.5821 3 1 874.5 289.5 0.0574 0.1292 0.4447 0.2258 1.1846 3 2 874.5 289.5 0.0573 0.0527 1.0857 0.1684 1.1760 4 1 844.3 319.7 0.1306 1.3513 0.0967 1.3976 2.2659 4 2 849.2 314.8 0.1350 1.3332 0.1013 1.3773 1.9990 … | |
Re: You can get rid of encryption by converting your file to ps and back to pdf (using ghostscript for example). | |
Re: you should use : [code=python] >>> mystring[13:0:-1] '!dlroW ,olle' [/code] But the first letter isn't taken as the second parameter of the slice is to be excluded... To have the first letter (H), you'll need to do [code=python] >>> mystring[13::-1] '!dlroW ,olleH' [/code] ![]() | |
Re: use fnmatch module to filter : [url]http://docs.python.org/library/fnmatch.html[/url] os.rename to move your file | |
Re: You can do more efficient : [code=python] sql = "SELECT Name FROM nameDatabase" self.cursor.execute(sql) list=[element[0] for element in self.cursor.fetchall()] print list [/code] | |
Re: [CODE=python] if not ignore: new_list.append(line) [/CODE] |
The End.