rrashkin 41 Junior Poster in Training

pyTony's question is valid. Why split it up? But if you must, then my code above ought to work with a couple of changes. I forgot to open the output file for "write", and you probably want the first (header) row to be skipped:

        data=[]
        with open(<csv file name>) as fid:   
            lines=fid.read().split('\n')
        for i in lines[1:]: 
            data.append(i.split(','))
        data.sort(key=lambda x: x[3])
        for d in data:
            field=d[3]
            with open('pc_Numbers_'+field+'.csv','a') as fid:
                while d[3]=field: 
                    fid.write(d+'\n')
rrashkin 41 Junior Poster in Training

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 fid:
                while d[3]=field: 
                    fid.write(d+'\n')

This assumes the field in question is in the fourth position (index 3).

If the file is really too big for this, then you'll have to read and write each line, changing the file name based on the field. Depending on how many files you expect, you may have to open and close them each time. That could make the program very slow.

rrashkin 41 Junior Poster in Training

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).

rrashkin 41 Junior Poster in Training

From the Library Reference:

The set type is mutable — the contents can be changed using methods like add() and remove().

rrashkin 41 Junior Poster in Training

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

so:

def ymd2doy(y,m,d):
    if y<100: y+=2000
    ly=0
    if leapyr(y): 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

Is that what you meant?

rrashkin 41 Junior Poster in Training

Also, you cast Y to float() but not X so it remains a string: 2*"1"="11"
[never mind: I see you're in v3.3 so input should take care of the conversion properly. AS Gribouillis said...]

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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 qualified filename>)"

rrashkin 41 Junior Poster in Training

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)
Shaji_1 commented: Great starting point. Thank you! +0
rrashkin 41 Junior Poster in Training

get rid of "print(input(..."
name1=input("what is your name: ")

Gribouillis commented: indeed +14
rrashkin 41 Junior Poster in Training

Take a look at the 'struct' module.

rrashkin 41 Junior Poster in Training

@slate (not to hijack the post but...)
Why? I've seen that construction but why is it preferred?

rrashkin 41 Junior Poster in Training

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 of the class, "cA", and "chA()" allows "a" to be changed.

You can also allow attributes to be set in the object constructor, using init():

class guiApp:
  def __init__(self,master):
    frame=Frame(master)
    frame.grid()
    self.mkWgts(frame)
...
root=Tk()
app=guiApp(root)
rrashkin 41 Junior Poster in Training

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>
rrashkin 41 Junior Poster in Training
>>> 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}
rrashkin 41 Junior Poster in Training

So you want to embed the second loop inside the first loop? That's not the way it is now.

rrashkin 41 Junior Poster in Training

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]
rrashkin 41 Junior Poster in Training

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']
rrashkin 41 Junior Poster in Training

Let's say your string, QUESTION DEFINITIONS
var P1_1 = new Array("P1_1", "An audit charter should:", "A. be dynamic and change often to coincide with the changing nature of technology and the audit profession.", "B. clearly state audit objectives for the delegation of authority for the maintenance and review of internal controls.", "C. document the audit procedures designed to achieve the planned audit objectives.", "D. outline the overall authority, scope and responsibilities of the audit function.", "An audit charter should state management's objectives for, and delegation of authority to, IS audit. This charter should not significantly change over time and should be approved at the highest level of management. The audit charter would not be at a detail level and therefore would not include specific audit objectives or procedures.", 4, 1);
, is in a file called "text.txt" (it could be anything).
Run this code:

fid=open("text.txt","r")
strA=fid.read()
fid.close()
strA=strA.split("(")[1].split(")")[0]
strA=strA.replace(",","\n")
print strA

What do you get?

rrashkin 41 Junior Poster in Training

Did you use ActiveState to get Python? If not, what did you use?

rrashkin 41 Junior Poster in Training

So it seems that "QUESTION DEFINITIONS
var P1_1 = new Array...
level and therefore would not include specific audit objectives or procedures.", 4, 1);" is a string that does indeed look like a javascript array assignment. Let's say you've read that string into a python variable, strA. First, let's get rid of the stuff outside the parentheses:
strA=strA.split("(")[1].split(")")[0]
Now let's replace the commas with linefeeds:
strA=strA.replace(",","\n")
Now I think you have what you want, no?

rrashkin 41 Junior Poster in Training

What you've shown is an array (list), that is, it's already split. What are you starting with?

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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")
rrashkin 41 Junior Poster in Training

You have: from tkinter import *.
Should be: Tkinter

rrashkin 41 Junior Poster in Training

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" is for.

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

I'm guessing it's "wind direction"

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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, put reader=open(files) (open for reading is the default). Then change the close to reader.close().

rrashkin 41 Junior Poster in Training

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 recent call last):
  File "/base/data/home/apps/runpythoncode/103.348367701639469596/console.py", line 123, in _execStatement
    exec compiled in statement_module.__dict__
  File "<string>", line 1, in <module>
IndexError: list assignment index out of range

You can initialize the y list with blanks before you start the loop with list comprehension:

y=[" " for i in xrange(len(x))]

or by list multiplication:

y=len(x)*[" "]
rrashkin 41 Junior Poster in Training

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)

rrashkin 41 Junior Poster in Training

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]
Then the max word count is: max(list-2),
and the string in list-1 with that count is: list-1[list-2.index(max(list-2))]

rrashkin 41 Junior Poster in Training

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]:

rrashkin 41 Junior Poster in Training

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.

james.lu.75491856 commented: WRONG! os.system for all the commands in command line! not for launching programs! +0
Gribouillis commented: indeed +14
rrashkin 41 Junior Poster in Training

I don't really see what you're asking. What problem are you having?

rrashkin 41 Junior Poster in Training

sure. It's just a name.

rrashkin 41 Junior Poster in Training

look at this 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 files; on systems that don’t have this distinction, adding the 'b' has no effect.

rrashkin 41 Junior Poster in Training

assuming this is tkinter, you could use pack instead of grid and -side left or -side right.

rrashkin 41 Junior Poster in Training

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. Perhaps it is simpler just to instantiate a new obect.

rrashkin 41 Junior Poster in Training

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()
rrashkin 41 Junior Poster in Training

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()
rrashkin 41 Junior Poster in Training

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.

rrashkin 41 Junior Poster in Training

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 each desired element explicitly.

rrashkin 41 Junior Poster in Training

This is sort of buried in the language reference. If you look here: Click Here, 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 name: value" entries.

rrashkin 41 Junior Poster in Training

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_'