- 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
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]` … |