- Strength to Increase Rep
- +6
- Strength to Decrease Rep
- -1
- Upvotes Received
- 34
- Posts with Upvotes
- 27
- Upvoting Members
- 20
- Downvotes Received
- 1
- Posts with Downvotes
- 1
- Downvoting Members
- 1
de-frocked physicist
- Interests
- woodworking, dogs, politics
- PC Specs
- bodhilinux on EVEREX desktop and eeePC 701 netbook
75 Posted Topics
Re: Maybe: with open('c:\FLOUpper.txt', 'r') as infile,open('c:\FLOLower.txt', 'w') as outfile: data = infile.readlines() data = [i.capitalize() for i in data] outfile.write(data) | |
Re: I think there are 2 ways depending on just how big the file really is. If it can all be read into memory, then I suggest: data=[] with open(<csv file name>) as fid: lines=fid.read().split('\n') for i in lines: data.append(i.split(',')) data.sort(key=lambda x: x[3]) for d in data: field=d[3] with open('pc_Numbers_'+field+'.csv') as … | |
Re: It seems this, `blue = IntVar()`, defines "blue". I've had poor results trying to use any widget's textvariable in Tkinter (as opposed to Tk). I think you would need to explicitly set the variable, blue, in the entry widget, blue_text, `app.blue_text.insert(0,blue)` (I think that's the right syntax). | |
Re: From the [Library Reference](http://docs.python.org/2/library/stdtypes.html#set-types-set-frozenset): > The set type is mutable — the contents can be changed using methods like add() and remove(). | |
Re: Here's my 2 cents: Let's say I have a function that turns Year,month,day into day of year: def ymd2doy(y,m,d): if y<100: y+=2000 ly=0 if (y-2000)%4==0: ly=1 md=(31,28+ly,31,30,31,30,31,31,30,31,30,31) for i in xrange(m-1): d+=md[i] return d Instead of `if (y-2000)%4==0: ly=1` I want to use a function: def leapyr (y): return (y-2000)%4==0 … | |
Re: It can be done with the eval() builtin function. **However!!!** it is very dangerous to allow users to input any old thing and then evaluate that input. It would be better to give them a limited set of choices. | |
Re: Unless I missed it, it seems you haven't posted your code. It would be easier (read: maybe possible) to help if we could see what you are trying to do. If the code is long, maybe just the pertinent piece will suffice. | |
Re: I'm not sure what your particular needs might be but the standard way of accessing another script is "import <filename>". For that to work, the accessed file either needs to be in the same directory as the running script or in the sys.path. Another, less enthusiastically recommended, way is "execfile(<fully … | |
Re: get rid of "print(input(..." `name1=input("what is your name: ")` | |
Re: Take a look at the 'struct' module. | |
Re: 100000 isn't that big. Why not just: f=open(<your text file>) for line in f: if <your search string> in line: <do your thing> f.close() or even: f=open(<your text file>) strA=f.read() f.close() if <your search string> in strA: <do your thing> | |
Re: There are a couple of choices. You can initalize the object with some attiributes and include methods for changing them: #class test class cA(object): a = 42 def chA(self,b): self.a = b return (self.a,b) f=cA() print f.a x=f.chA(8) print x print f.a raw_input('done') In this case "a" is an attribute … | |
Re: >>> d={"key1":3,"key2":18,"key3":8} >>> d["key2"]=0 >>> d {'key3': 8, 'key2': 0, 'key1': 3} >>> s=d.items() >>> s [('key3', 8), ('key2', 0), ('key1', 3)] >>> for i in s: ... if i[1]==0: ... del d[i[0]] ... >>> d {'key3': 8, 'key1': 3} | |
Re: So you want to embed the second loop **inside** the first loop? That's not the way it is now. | |
Re: Are you allowed to use list comprehension which is an implicit for loop? [i*6 for i in xrange(3,0,-1)] [18, 12, 6] | |
Re: Did you use ActiveState to get Python? If not, what did you use? | |
Re: I'm not sure how you're supposedto use re.finditer but not this way. The elements of the returned list are match objects, not strings. I suggest you use findall instead. If I do this: lst1=re.findall(r'rhs="(.*)"',d1) I get this: ['domain.com', 'domainn.com', '1010data.com'] | |
Re: What you've shown is an array (list), that is, it's already split. What are you starting with? | |
Re: Not to endorse or refute the underlying concept, if you provided the rules (eg: number of vowels is greater than twice the square root of the number of "s"s) I'm sure you'd get some help. | |
Re: You have: from tkinter import *. *Should* *be*: **T**kinter | |
Re: If the file is small enough (and that depends on your memory, usually pretty big) to be read all at one go: f=open(<text file name>) strdata=f.read() f.close() c=strdata.count("abc") | |
Re: Everything is a function except `cSetInsert(5,4)` so that gets executed. cSetInsert tries to use "5" as the argument, "cSet". However the function treats it as a list: `cSet[hashChar(e)]` I think you intended to call cSetCreate() first and pass in the corresponding returned cSet. I don't know what you think "5" … | |
Re: If I understand correctly you're having a problem parsing the return of "version.sh" which looks like the 15-line report. Try this: Insert after "output=commands.....": outlist=output.split('\n') for i in outlist: if i.startswith("Version"): vers=i.split()[1] if i.startswith("Type"): type=i.split()[1] Then make your "result=..." however you want to format vers and type. | |
Re: random.choice() will return a single element. Why are you making it a tuple? If you are trying to get the key **and** value out, you will need to do it explicitly: `k=random.choice(d_mod); v=d_mod[k]; arr[y].append([k,v])` if that's what you really want. | |
Re: You print the index after the value is incremented: index += 1 print "index: " +str(index) So the loop is iterated with the value of "index" equal to 11, then the value of index is increased but the loop is not evaluated so the processing doesn't cease. | |
Re: Let's say d is a date string in the format you posted. Then `m=datetime.date(*map(int,d.split("/"))).month ` returns the month as an integer. You could construct a dictionary that collects the data for each month: `dctTemp[m].append(newTemp)` Then you can average the values when you collect all the days. | |
Re: I've used both and I make my decision as follows: I use Tkinter on Windows since Python installs with it and I use GTK on Linux (Ubuntu) since that's what comes with that. | |
![]() | Re: First of all, you probably don't need to close and open "writer" at every step. It's just creating a lot of overhead. Open it once as you do before the loop and close it once afterward. As for the reading files, instead of `writer=open('psub', 'a')`, which I recommend you remove, … |
Re: Within the function, you do, indeed, need to initialize y before you can start defining elements. Even then, if you try to assign elements out of order, that is, an element, [i], when [i-1] is empty, you will get the out of range error: >>> y=[] >>> y[8]=5 Traceback (most … | |
Re: Can you assume that each word is separated by a space (one and only one), and that there are no leading and trailing spaces? If so, let's say your list of strings is "list-1". Then you could make a list of word counts as: `list-2=[i.count(" ")+1 for i in list-1]` … | |
Re: I'm guessing you're using Python 3.x where print() takes exactly one argument. Instead of `print (x)('x')(y)` you need something like ("something like" because I don't have Python 3 to test on): `print(x + 'x' + y)` | |
Re: I would start by reading the file into a list: fid=open('file1.txt') lstData=fid.readlines() fid.close() Now the last record is: lstData[-1] To loop through the records from the 2nd to last until the first, `for i in lstData[-2::-1]:` | |
Re: I'm not really clear what you're trying to do but os.system has fallen from favor and the subprocess module is recommended. Maybe that will suit your needs better. | |
Re: I don't really see what you're asking. What problem are you having? | |
Re: look at [this](https://skydrive.live.com/redir?resid=F5DA0DF892D264EE!675&authkey=!ABykCFZUlTggjxI) for an example. Basically, the key is in the **open()**: > Modes 'r+', 'w+' and 'a+' open the file for updating (note that 'w+' truncates the file). Append 'b' to the mode to open the file in binary mode, on systems that differentiate between binary and text … | |
Re: assuming this is tkinter, you could use **pack** instead of grid and **-side left** or **-side right**. | |
Re: Well, when you set enemy equal to goblin, that's really just an association, not a new object. You can see an explanation of the issue and the use of "copy" to deal with it [here](http://docs.python.org/2/library/copy.html?highlight=deep%20copy). Perhaps it is simpler just to instantiate a new obect. | |
Re: If you don't insist on using RE, fid=open(<csv file>) chklst=["Red Hat","Debian" , "Fedora" , "Linux"] for row in fid: for o in chklst: if o in row: print row break fid.close() | |
Re: For what you describe, I would eschew the csv module. Generally, split and append: fid=open(<filename>) lstOfLines=[] for record in fid: lstOfLines.append(record.strip("\n").split(",")) fid.close() | |
Re: Either make the appropriate variables class variables (by declaring them at the class level) or pass values (arguments other than "self") into the function(s) and return the computed result as a variable. | |
![]() | Re: First of all, unzip step doesn't really help you. You can get the ith element of each list with list comprehension: `list=[a[i] for a in column]` for a given i. To get some other set of elements as you describe will require looping through the **number** of elements and taking … |
Re: This is sort of buried in the language reference. If you look here: [Click Here](http://docs.python.org/2/glossary.html#term-argument), it gets explained, but you have to dig. In particular: > or passed as a value in a dictionary preceded by ** That means that the identifier after the "**" is a dictionary with "variable … | |
Re: I can't really follow what you're doing but if you want to print a string with all the "0"s replaced with "_" you can "replace": `>>> "11110abc0".replace("0","_") '1111_abc_'` | |
Re: It's very ineffient but here goes: a= [10, 5, 2, 7, 20] b=[] for i in xrange(len(a)): b.append(min(a)) a.pop(a.index(b[-1])) | |
Re: Consider this: last_column = [i[-1] for i in big_list] | |
Re: The maximum height is where yvel = 0. In your initialization method you have: `self.yvel=velocity*sin(theta)` You know that yvel goes to zero when 0.98*time equals the initial velocity, or at `velocity*sin(theta)/9.8 seconds` So you can figure out when you get to that time at your interval. Now since xvel is … | |
Re: First a list of all the gemstones: f=open("your file name") gemstones=[] for i in f: gemstones.append(i.split(',')[1]) f.close() now a dictionary where key is gemstone and value is count gcount={} for g in gemstones: gcount[g]=gcount.get(g,0)+1 | |
Re: I dont use WX but I don't think that matters. There's a lot of code there and I admit I didn't read it all. So I don't know really what you're doing already, but you have asked a specific question. I think I would make each sensor a different obect … | |
Re: Your data is in a 2-d list, data_list. The first step is finding the indices of the 2 months you want. That will accomplished on data_list[0]. index1=data_list[0].index(month1), where month1 is the starting month. Likewise, index2=data_list[0].index(month2). Now you'll loop through the elements from 1 to the end, convert the string rainfall … | |
Re: I would need more information to answer that. What is the structure of the dictionary? Normally, I would key an address book on "name" so something like AddBook={"john doe":("8134789 Zubruzlechester", "Alpha Centauri", "999-555-1234")}. In that case I would get the telephone number, for instance, for John Doe, as: AddBook["john doe"][2] |
The End.