Gribouillis 1,391 Programming Explorer Team Colleague

Using [charD]*len() as an argument to map() is clever, but in this context is a very poor "example of the map function" because it is so unPythonic.
In 2.4 is better (and simpler) to do:

charD = dict([ (n, chr(n)) for n in range(32, 256) ])

replacing five lines (including the import ) with one, and a simple one at that. Perhaps a better title for this snippet would be 'an example of functional programming style in Python'.

I think this is unfair: before list comprehensions became the rule, using map was considered a very pythonic way to do. You should better ask Guido Van Rossum why he didn't invent list comprehensions earlier. The language changes slowly and there is no point in reviving old snippets just to say that they are poor programming style.
Furthermore, map exists in many functional and non functional languages (lisp, ocaml, haskell, perl ...) and at a time, it sounded like a quite natural feature in python.

Gribouillis 1,391 Programming Explorer Team Colleague

I have another question about counting so i'll use this theme for it.

I have nubers list = [85, 25, 56, 11, 57, 52, 47, 72, 11, 94];

Average is 51 and need to count how many numbers from this list are bigger than average. How can I do this?

In python, the boolean True has integer value 1, so you can write

>>> L = [85, 25, 56, 11, 57, 52, 47, 72, 11, 94]
>>> sum((x > 51) for x in L)
6
Gribouillis 1,391 Programming Explorer Team Colleague

Well, here is the code I use to transfer files with paramiko, ssh and sftp

import paramiko

COMP = "computername"

ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(COMP, username="myusername", password="mypassword", allow_agent = False)

src = "/cygdrive/c/Documents*Settings/myusername/myfile.txt"
dst = "/home/myusername/myfile.txt"

ftp = ssh.open_sftp()
ftp.get(src , dst)
ftp.close()

To upload a file to the host, call ftp.get(dst, src)

TrustyTony commented: Post as code snippet also, pal! +2
Gribouillis 1,391 Programming Explorer Team Colleague

At line 8, it should be print test .

Gribouillis 1,391 Programming Explorer Team Colleague

Here is an more complete example

>>> L = [ (1, (2, 3)), (4, (5, 6)), (7, (8, 9))]

>>> for item in L:
...     print  item
(1, (2, 3))
(4, (5, 6))
(7, (8, 9))
    
>>> for x, y in L:
...     print x, y
1 (2, 3)
4 (5, 6)
7 (8, 9)
    
>>> for x, (y, z) in L:
...     print x, y, z
1 2 3
4 5 6
7 8 9

(examples obtained with the recent reinteract python shell. Try it, it looks promising).

Gribouillis 1,391 Programming Explorer Team Colleague

You already open("exercise1.txt", "w") outside your loop. This creates a new empty file. Good. But you also open the same file in your for loop. Don't do that. Repeating the open statement in 'w' mode for that file recreates the empty output file each time it reads a line from your input file, thus erasing any content you have written to it.

There are other issues. After inFile.readlines() at line 14, the file position is at the end of the file, so the for i in inFile iterates over nothing: the loop's body is never executed.

Gribouillis 1,391 Programming Explorer Team Colleague

This session of the python interpreter should enlight you

>>> grades = raw_input("Enter grades:")
Enter grades:10, 4, 4, 2
>>> print grades
10, 4, 4, 2
>>> # grades is a string (str). To examine it we print its repr
... 
>>> print repr(grades)
'10, 4, 4, 2'
>>> # the string is separated by commas. Let's split it
... 
>>> gradelist = grades.split(",")
>>> print gradelist
['10', ' 4', ' 4', ' 2']
>>> # gradelist is a list of strings. Some of them have white space
... # around the numbers. Let's remove this white space
... 
>>> gradelist = [s.strip() for s in gradelist]
>>> print gradelist
['10', '4', '4', '2']
>>> # now let's convert these strings to floating point numbers
... 
>>> gradelist = [float(s) for s in gradelist]
>>> print gradelist
[10.0, 4.0, 4.0, 2.0]
>>> # let's sum these numbers
... 
>>> bigsum = sum(gradelist)
>>> print bigsum
20.0
>>> # what is the number of grades ?
... 
>>> print len(gradelist)
4
>>> # let's compute the average
... 
>>> average = bigsum/len(gradelist)
>>> print average
5.0
vegaseat commented: nice +13
Gribouillis 1,391 Programming Explorer Team Colleague

The with statement allows all sorts of syntactic sugar in python. This snippet defines two contexts: inevitably and preferably which register a function call to be executed at the end of a block of statement. Although they are not very useful (the function call could be written directly at the end of the block, or with the help of a try...finally structure), some programmers may appreciate this style in certain circumstances.

Gribouillis 1,391 Programming Explorer Team Colleague

You can also write Z = [x+y for x in X for y in Y] .

vegaseat commented: sweet +13
Gribouillis 1,391 Programming Explorer Team Colleague

You should consider creating an image instead of a traditional plot. Some people here in the python forum manipulate the PIL module and other photograph handling modules. You could plot a pixel in an image for each of your points. Unfortunately I don't use PIL, and I can't help you much, but the idea should work.

Gribouillis 1,391 Programming Explorer Team Colleague

I remember a restriction in the threading module, that the __init__ function of a user subclass of Thread should call Thread.__init__(self) before any other statement. Could there be a similar restriction for the Process class ?

Gribouillis 1,391 Programming Explorer Team Colleague

I have tried to keep my programs simple, but I find that everything I want to do requires mixing of languages
thanks anyhow for the advice
python01

If you easily want to pass values from one language to another, you should use the json format. Most languages (including python) can parse and write json data. See the json module documentation http://docs.python.org/py3k/library/json.html#module-json

Gribouillis 1,391 Programming Explorer Team Colleague

If you are new to python, avoid mixing python, java and javascript.

Gribouillis 1,391 Programming Explorer Team Colleague

It's pretty simple: a server is a program which runs on a machine and waits for clients to 'connect' to a port on this machine. A client is a program (usually running on another machine) which knows the name or the IP address of the server's machine and the port number where the server process is listening, and uses this information to connect to the server.

Once the server and the client are connected, the 2 programs start sending information to each other through a special software component called a socket. The information itself has the form of a stream of bytes.

There are numerous variations on this basic scheme: the client and the server must agree on the structure and the meaning of the character strings that they exchange. This is called a protocol and there are hundreds of protocols: http, ftp, ssh, etc. Second thing: to ease the programmer's perspective, the socket is often hidden in higher level software components. For example python has several modules to communicate on the web, which implement different forms of servers and clients.

When you can use a higher level component (for example the module paramiko to communicate with the ssh and sftp protocols), it's better to use them than to use raw sockets. With raw sockets, you may easily reinvent the wheel (and probably fail to do so) :)

Gribouillis 1,391 Programming Explorer Team Colleague

I thought users of the python forum could be interested in a seminar conference by Guido van Rossum about the design and implementation of the python language, so here is the link

http://irbseminars.intel-research.net/GuidoVanRossum.wmv

(it may be easier to download the file first instead of reading it directly from your browser)

vegaseat commented: thank you +13
Gribouillis 1,391 Programming Explorer Team Colleague

I'm not exactly sure on how this works. Can you please elaborate? Thanks a lot.

My main problem is how the exec statement really work and why is it name in self.__dict__, and how the functions got appended into the Unifier object

It's easy to understand. A statement like from socket import * is always executed
in a namespace, and the symbols of module socket are added to this namespace. The namespace can
be any dictionary. When you write this in your program, the namespace is usually the namespace of
the current module, but you can write

D = dict()
exec "from socket import *" in D

and the symbols from the socket module are added to the dictionary D instead of the current namespace.
In the Unifier class, I add the symbols to the Unifier instance's __dict__ dictionary,
so that the symbols can be accessed as the instance's attributes.
Here is a better Unifier class where you can unify with module names or other unifier objects

# python 2
"""module modunifier.py"""

class Unifier(object):
    """Unifier(*items) -> a new unifier object.

    @items: a sequence of (possibly compound) module names or Unifier instances.
    All the modules exportable names become attributes of the unifier object.
    
    """
    def __init__(self, *items):
        self += items
    def __iadd__(self, items):
        for item in items:
            if isinstance(item, Unifier):
                self.__dict__.update(item.__dict__)
            else:
                # if python 3, replace this by builtin function exec.
                exec "from %s import *" % item in self.__dict__
        return self …
Gribouillis 1,391 Programming Explorer Team Colleague

The problem is that functions in a and b can be dynamic and up to n amount of functions, even the filenames can be dynamic.

and they all need to be unified.

Then create a specialized class

# python 2
class Unifier(object):
    def __init__(self, *module_names):
        self += module_names
    def __iadd__(self, module_names):
        for name in module_names:
            # if python 3, replace this by builtin function exec.
            exec "from %s import *" % name in self.__dict__
        return self

Then you can write

c = Unifier("a", "b")
# or
c = Unifier()
c += ["a", "b", "os.path"]

Warning: this may fail in the very unlikely case when one of the modules contains a function __iadd__.

TrustyTony commented: Looks wicked! +2
Gribouillis 1,391 Programming Explorer Team Colleague

There was an old solution on linux to kill a subprocess

# linux, mac
os.kill(pid, signal.SIGKILL)
killedpid, stat = os.waitpid(pid, os.WNOHANG)
if killedpid == 0:
    print >> sys.stderr, "ACK! PROCESS NOT KILLED?"

however the Popen.terminate() method is more recent and probably does about the same. Also what you can do is try to read the stdout and stderr of your subprocess while it is running. I once found a very simple method to do this in linux. See this post http://www.daniweb.com/forums/post777876.html#post777876.

Gribouillis 1,391 Programming Explorer Team Colleague

In a loop, the iter method is called: see this experiment

>>> x = xrange(10)
>>> x
xrange(10)
>>> y = iter(x)
>>> y
<rangeiterator object at 0x7fcbaaff2ed0>
>>> y.next
<method-wrapper 'next' of rangeiterator object at 0x7fcbaaff2ed0>
Gribouillis 1,391 Programming Explorer Team Colleague

You can use enumerate. Supposing you have 2 lists, keywords and word_list,

keyword_set = set(keywords) # better use a set
for i, w in enumerate(word_list):
    if w in keyword_set:
        word_before = word_list[i-1] if i > 0 else ''
        word_after = word_list[i+1] if i+1 < len(word_list) else ''
        print("%s <%s> %s" % (word_before, w, word_after))
Gribouillis 1,391 Programming Explorer Team Colleague

You can iterate over the words, or build a list, with a regular expression (the re module)

import re

abstract = """hello, my name is george. How are you? Today
i am not feeling very well. I consider myself to be
sick."""

word_pattern = re.compile(r"\w+")

print list(word_pattern.findall(abstract))

""" my output -->
['hello', 'my', 'name', 'is', 'george', 'How', 'are', 'you', 'Today', 'i', 'am', 'not', 'feeling', 'very', 'well', 'I', 'consider', 'myself', 'to', 'be', 'sick']
"""
Gribouillis 1,391 Programming Explorer Team Colleague
vegaseat commented: thanks for the help +12
Gribouillis 1,391 Programming Explorer Team Colleague

This program is 3000 lines long. It can easily be reduced to 500 lines by refactoring. I think this is the first step before you add new features.

Beat_Slayer commented: Well said. Good point for programming. +1
Gribouillis 1,391 Programming Explorer Team Colleague

ok that worked just fine, but im trying to make a "who wants to be a millionaire" sort of game, so how can I use this example and turn it in to a window that has the question with 4 buttons, 3 of them will be wrong answer, and the other one the correct answer?

Try to create several buttons, with different parameters.

Gribouillis 1,391 Programming Explorer Team Colleague

You can use os.walk this way

import os

def must_open(dirpath, filename):
    """change this function to select if a file must be opened or not"""
    return True

def opened_files(*args):
    """generate a sequence of pairs (path to file, opened file)
    in a given directory. Same arguments as os.walk."""
    for dirpath, dirnames, filenames in os.walk(*args):
        for filename in filenames:
            if must_open(dirpath, filename):
                filepath = os.path.join(dirpath, filename)
                yield (filepath, open(filepath, "rU"))

# main loop:
mydir = "/home/user/path/to/my/dir"
for filepath, file in opened_files(mydir):
    # do something
Malinka commented: Thank you. Your solution works! +1
TrustyTony commented: nice opened_files generator +2
Gribouillis 1,391 Programming Explorer Team Colleague

This is a recurrent question in this forum: use the search form at the top of the page.
Otherwise, read 'dive into python' online. You can also read the more than 200 entries in the thread 'starting python' above, and the same number of posts in 'projects for the beginner': the best way to learn is to write small programs yourself.

Gribouillis 1,391 Programming Explorer Team Colleague

Nice work. You may be interested in the python program grin http://pypi.python.org/pypi/grin, which is multiplatform, but produces a result similar to unix' grep command. A nice feature of grin is that it uses python regular expressions in the query. Also its source code may contain ideas to improve your program.

Gribouillis 1,391 Programming Explorer Team Colleague

Figured that out. Even if we have the Python script in the same directory or we add the directory in which the python file exist in PYTHONPATH, the C program might not be able to recognize that. In such case, just add the following lines immediately after Py_Initialize(); :

PyRun_SimpleString("import sys");
    strcpy(syspath, "sys.path.append('");
    if (getcwd(pwd, sizeof(pwd)) != NULL)
        strcat(syspath, pwd);
    strcat(syspath, "')");
    PyRun_SimpleString(syspath);

This is similar to adding sys.path from Python console.

But it will work only if dummy.py is in the current working directory. You should use the directory which contains dummy.py instead...
You can also use strcpy(syspath, "import sys;sys.path.append('"); and run PyRun_SimpleString only once.

Also if the directory is in PYTHONPATH, you can try PyRun_SimpleString("import dummy") . This will put dummy in sys.modules and your second import in C should work...

Gribouillis 1,391 Programming Explorer Team Colleague

About python 3, you should be able to install it with yum install python3 .

Gribouillis 1,391 Programming Explorer Team Colleague

Try this command

python -c "import os, idlelib; execfile(os.path.join(os.path.split(idlelib.__file__)[0], 'idle.py'))"&

It should be a multiplatform command. If you can't import idlelib, it means that you must install the tkinter package.
Note that this simpler command also works

python -c "from idlelib import idle"&
Gribouillis 1,391 Programming Explorer Team Colleague

The id retrieval idea looks good. Il give it a try. Maybe it would be possible, that knowing the object id, to retrieve its name.
Although it seems to be a practical way to solve my problem, I don't want to pass the name of the object to the instance itself as an argument because this is what I'm doing now.
The idea of a dictionary to hold the names and the objects id also sounds good. But I am curious about my original question: it is possible, from inside an instance of a class to retrieve the instance name? Let's say I've instantiated x as an instance of the class MyClass(); It is possible from inside x to retrieve "x" which is the name of the instance as a string?

Best regards,

PTS

The instance doesn't have a name. When you write

x = MyClass()

x is not the name of the instance, x is a variable name in the local namespace, which is temporarily bound to your instance. If you call func(x) and func is defined by

def func(item):
  print(item)

func manipulates the same instance but it's bound to the local variable named 'item', so it is the same instance, but a different name.
If you need to identify your instance with a string, there are only 2 ways: either you explicitely create a name for the instance, or you build a name based on the instance's id. For example, you can build …

vegaseat commented: good comment +12
Gribouillis 1,391 Programming Explorer Team Colleague

The Timer.timeit function runs your function 1 000 000 times by default, so your function runs in 21 microseconds on average.
If you want to run a program only once and measure its performance, you should use the 'cProfile' or 'profile' module. See http://docs.python.org/library/profile.html.

Gribouillis 1,391 Programming Explorer Team Colleague

You should create a list for your owner_id:

class sendbackcurricula(app.page):
    def GET(self):
        query = models.Curricula.query.all()
        map = dict()
     	for x in query:
            x = x.to_dict()
            key, title = x['owner_id'], x['title']
            if key not in map:
                map[key] = list()
            map[key].append(title)
        return json.dumps(map)

This should return {"email": ["Course1", "Course2", "Course3"] }.

Gribouillis 1,391 Programming Explorer Team Colleague

I suggest this

from random import randrange

def randhex(hexdigits):
    s = hex(randrange(16 ** hexdigits))
    if s[-1] == 'L':
        s = s[:-1]
    return s

for i in range(10):
    print(randhex(8))

""" my output -->
0xae662345
0xd2b915f5
0x244435bd
0xdf1c08e
0x73d6018f
0xf4b05b97
0x589e2a82
0xad3a2ea9
0xea758570
0xbe48582d
Gribouillis 1,391 Programming Explorer Team Colleague

I figured i would employ your recomendations from the top down, beginning with 'optparse'. The doc was very difficult to follow, but I found this link; http://www.alexonlinux.com/pythons-optparse-for-human-beings, and if finally started making sense. Not that it didn't, I just think the Python doc was weak for someone not familiar with it.

Yes there is a learning phase for using optparse, but it is worth the effort: after 2 or 3 scripts using optparse, you will see the benefits: options are easily added, modified and handled by your program. Optparse is a very complete tool. You don't need to master all of its aspects to use it. Learn the minimum for your program.
There is a (non conventional) example use of optparse in this code snippet http://www.daniweb.com/code/snippet258640.html (it may be more difficult to understand than your excellent tutorial).

Gribouillis 1,391 Programming Explorer Team Colleague

First avoid 'list' as a variable name since it is the name of a builtin type. There are at least 2 ways to do it

old_list = ["a","b","c","d"]

new_list = list()
for x in old_list:
    new_list.append(ord(x))

or, using 'list comprehension syntax'

old_list = ["a","b","c","d"]
new_list = [ ord(x) for x in old_list ]
Gribouillis 1,391 Programming Explorer Team Colleague

The problem is that raw_input returns a string and not an integer. You must convert it to integer with the int function. See this example

>>> x = raw_input("enter a number: ")
enter a number: 2
>>> print x, repr(x), type(x)
2 '2' <type 'str'>
>>> x = int(x)
>>> print x, repr(x), type(x)
2 2 <type 'int'>
Gribouillis 1,391 Programming Explorer Team Colleague

re.search returns a 'match object' or None. Follow this example

>>> mo = re.search(r"b\w+", "'Give me bacon and eggs,' said the other man.")
>>> mo
<_sre.SRE_Match object at 0x7f90947e2e68>
>>> mo.group(0)
'bacon'
Gribouillis 1,391 Programming Explorer Team Colleague

I managed adding an element this way

from lxml import etree
from lxml.builder import ElementMaker


E = ElementMaker()

DOC = E.doc
PERSON = E.activity
TITLE = E.title
DESC = E.desc
IDNO = E.idno
def NAME(*args):
    return {"name":' '.join(args)}

file = "xmltestthing.xml"
parser = etree.XMLParser(remove_blank_text=True) # see http://codespeak.net/lxml/FAQ.html#parsing-and-serialisation
thing1 = etree.parse(file, parser)

person = PERSON( NAME("Dani"),
    DESC ("the queen"),
    IDNO ("5")
    )

doc = thing1.getroot()
doc[-1].addnext(person)
print(etree.tostring(doc, pretty_print=True))

""" My output -->
<doc>
  <activity name="John">
    <desc>germedabob</desc>
    <idno>2</idno>
  </activity>
  <activity name="Dani">
    <desc>the queen</desc>
    <idno>5</idno>
  </activity>
</doc>
"""
happymadman commented: Thanks +2
Gribouillis 1,391 Programming Explorer Team Colleague

Unfortunately, the os.startfile function exists only in Windows. A multiplatform way is

import subprocess as sp
sp.call(["python", "textversion.py"])

Or, even better

import subprocess as sp
import sys
returncode = sp.call([sys.executable, "textversion.py"])
TrustyTony commented: Beauty FAQ answer (solution 2) +1
Gribouillis 1,391 Programming Explorer Team Colleague

Well, here is an example

class Thingy(object):
    instances = []

    def __init__(self):
        self.instances.append(self)

def waste_time_and_memory():
    t = Thingy()

for i in range(5):
    waste_time_and_memory()

print Thingy.instances

""" My output -->
[<__main__.Thingy object at 0x7f0581777c50>, <__main__.Thingy object at 0x7f0581777c90>, <__main__.Thingy object at 0x7f0581777cd0>, <__main__.Thingy object at 0x7f0581777d10>, <__main__.Thingy object at 0x7f0581777d50>]
"""

The main problem is that Thingy objects are immortal unless you empty the list periodically.

danielochicaiza commented: Just joined to say thanks for this workaround. I think I will continue finding nice solutions in this place. +0
Gribouillis 1,391 Programming Explorer Team Colleague

To test if a path is a file, you can use

import os
os.path.isfile(path) # is it a file ?
os.path.isdir(path) # is it a folder ?
os.path.exists(path) # does it exist in the file system ?
Gribouillis 1,391 Programming Explorer Team Colleague

I think you should not start with a complicated gui interface. You could use simple tables like in this google code project http://code.google.com/p/prettytable/ to display the monthly schedule (it can generate html to view the table in your browser too). With this simple output, concentrate on the math of the problem.

Gribouillis 1,391 Programming Explorer Team Colleague

An more common alternative is

>>> L = ['cmds.sphere(n = "aBall", r = 1)', 'cmds.sphere(n = "aBall", r = 2)', 'cmds.sphere(n = "aBall", r = 3)', 'cmds.sphere(n = "aBall", r = 4)', 'cmds.sphere(n = "aBall", r = 5)', 'cmds.sphere(n = "aBall", r = 6)']
>>> import cPickle as Pickle
>>> s = Pickle.dumps(L) # list to string
>>> s
'(lp1\nS\'cmds.sphere(n = "aBall", r = 1)\'\np2\naS\'cmds.sphere(n = "aBall", r = 2)\'\np3\naS\'cmds.sphere(n = "aBall", r = 3)\'\np4\naS\'cmds.sphere(n = "aBall", r = 4)\'\np5\naS\'cmds.sphere(n = "aBall", r = 5)\'\np6\naS\'cmds.sphere(n = "aBall", r = 6)\'\np7\na.'
>>> print Pickle.loads(s) # string to list
['cmds.sphere(n = "aBall", r = 1)', 'cmds.sphere(n = "aBall", r = 2)', 'cmds.sphere(n = "aBall", r = 3)', 'cmds.sphere(n = "aBall", r = 4)', 'cmds.sphere(n = "aBall", r = 5)', 'cmds.sphere(n = "aBall", r = 6)']
Gribouillis 1,391 Programming Explorer Team Colleague

It's not caught because the line session = smtplib.SMTP(smtpserver) is out of the try..except statement !

Gribouillis 1,391 Programming Explorer Team Colleague

Point one
You can do comparison with tuples of numbers in Python

>>> (0,0)<(1,1)<(2,2)
True
>>> (0,0)<(1,1)<(2,0)
True
>>>

First is fine for me, but I am not agreeing with the second one. Why?

If a<b<c, then number b is between a and c in the number line.

(x,y) is point in two dimensional plane. For me the natural extension of < is for it to mean that

(a,b) < (c,d) < (e,f)

means that (c,d) is inside a square whose corners are (a,b) and (e,f). That is same as

a<c<e and b<d<f

The problem with your 'natural extension' of < to points in the plane is that you are defining a ternary relation P < Q < R and not a binary relation P < Q. The 'less than' relation is binary. Mathematically, it is an order relation (see http://en.wikipedia.org/wiki/Order_theory) which satisfies (much more) natural properties that you would expect from an ordering relation, for example P < Q and Q < R imply that P < R. In python (and also in mathematics), a < b < c means (a < b and b < c).

Python's ordering for tuples is called lexicographic order in mathematics. It is consistent with the usual ordering of words, for example tuple("hello") < tuple("world") , and it is also commonly used to order points in the plane. I'm afraid that your ternary relation can not be used to build a coherent binary relation.

vegaseat commented: good explanation +10
Gribouillis 1,391 Programming Explorer Team Colleague

It works perfectly!!!! Thank you so much:-)
What do you want for it? I can pay you via my paypal account, would that work for you?

Best regards, Casper:-)

It's ok. It's only a small exercise for most members of the python forum :)

Also, there is a little theoretical problem: your time samples are not uniformly distributed over the 1 second time intervals, so are you sure that a raw averaging is meaningful ? Shouldn't we design a special formula for the average over 1 second ?

vegaseat commented: nice way to handle this +10
Gribouillis 1,391 Programming Explorer Team Colleague

Ok, I renamed your file inputfile.txt and the code below produces an outputfile.txt containing the averages.

# averages.py
from itertools import groupby

def gen_averages(input_triples):
    for second, triples in groupby(input_triples, lambda triple: int(triple[2])):
        a, b = 0.0, 0.0
        n = 0
        for x, y, t in triples:
            a += float(x)
            b += float(y)
            n += 1
        yield (a/n, b/n, second)

def gen_input(textfile):
    f = open(textfile)
    f.readline()
    for line in f:
        triple = tuple(float(x) for x in line.strip().replace(",", ".").split())
        assert(len(triple) == 3)
        yield triple

def compute_averages(inputfile, outputfile):
    with open(outputfile, "w") as out:
        for triple in gen_averages(gen_input(inputfile)):
            out.write("{0:<15.2f}{1:<15.2f}{2:d}\n".format(*triple))
if __name__ == "__main__":
    compute_averages("inputfile.txt", "outputfile.txt")
    print "done (see the output file)"

To run the program, you have 2 solutions: either you start IDLE (python gui), then you open averages.py (the name of the program) and you select 'Run Module' in idle's run menu, or you open a windows cmd shell, you go to the folder containing the program and you type python averages.py on the command line.

The program must be modified if you want to read and write excel files instead of text files.

Gribouillis 1,391 Programming Explorer Team Colleague

As a starting point, here is a function which computes the averages for a sequence of values (parameter A, parameter B, time). The assumption is that the input data are sorted by time. Try to run it and see if it does what you want

from itertools import groupby

def gen_averages(input_triples):
    for second, triples in groupby(input_triples, lambda triple: triple[2]):
        a, b = 0.0, 0.0
        n = 0
        for x, y, t in triples:
            a += float(x)
            b += float(y)
            n += 1
        yield (a/n, b/n, second)

def test_me():
    data = [
        (1, 1, 364),
        (2, 4, 365),
        (3, 5, 365),
        (8, -1, 365),
        (3, 2, 366),
        (1, 1, 366),
        (0, 0, 367),
    ]
    for triple in gen_averages(data):
        print(triple)

if __name__ == "__main__":
    test_me()

""" My output -->
(1.0, 1.0, 364)
(4.333333333333333, 2.6666666666666665, 365)
(2.0, 1.5, 366)
(0.0, 0.0, 367)
"""

It only remains to read the data in the excel files and perhaps write the output to other excel files. We can use the modules xlrd xlwr xlutils for this.
What are your OS and your version of python ? Also, can you attach an excel file (or a part of it) to a post (you may have to zip it first) ?

Gribouillis 1,391 Programming Explorer Team Colleague

Oh, wait, it's not completely solved:

on linux, everything is fine.

on windows, however, webbrowser.open only opens Explorer, even though firefox is the default.

If I try startfile, it does open the associated program as d5e5 explained, but it fails to open the help file (it seems it only works with URL's)

To make sure you open firefox in a cross platform way, use webbrowser.get

import webbrowser
webbrowser.get("firefox").open("http://www.python.org")
vegaseat commented: thanks +10