Gribouillis 1,391 Programming Explorer Team Colleague

The command must be run in the terminal, not in python.

Gribouillis 1,391 Programming Explorer Team Colleague

it worked, but when i tried the

python /usr/lib64/python2.7/idlelib/idle.py

bit, it didn't work.

Does the file exist ? what was the error message ? Try

>>> import idlelib
>>> print idlelib
Gribouillis 1,391 Programming Explorer Team Colleague

i ran the command, didn't do anything. after restarting my machine python still doesn't import polymod. according to the terminal the PYTHONPATH is set right, so why wont it work? i dont get it

If the command didn't complain, it means that it worked. Type python in the terminal, and then

>>> import polymod
>>> dir(polymod)

to see if it works. Then you can try

python /usr/lib64/python2.7/idlelib/idle.py

in the terminal and then import polymod (replace the path to wherever your idle.py is).

Gribouillis 1,391 Programming Explorer Team Colleague

You can also try

python -c "import polymod"

in the terminal to see if it complains.

Gribouillis 1,391 Programming Explorer Team Colleague

no i start python using synapse, should i be starting python from the terminal?

the python path is as it should be

wolf@p:~$ echo $PYTHONPATH
/home/wolf/Desktop/MyModules

Normally no, you don't need to start python from the terminal, but if you changed your .bashrc since the last login, it may be different. Does it work in the terminal ?

You can also check this from python to see if PYTHONPATH worked

>>> import os
>>> print os.environ["PYTHONPATH"]

and also this, to see if MyModules is on the path

import sys
print sys.path
Gribouillis 1,391 Programming Explorer Team Colleague

If you changed the ~/.bashrc since you logged in, did you start python from the terminal where you ran the bash command after changing .bashrc ?

You can also type 'echo $PYTHONPATH' in the terminal to check that the pythonpath is correct.

Gribouillis 1,391 Programming Explorer Team Colleague

thanks for your help, when i started up today i created a new directory on my desktop with the name MyModules, worked immediately. i get the feeling there was something more t the problems i was having yesterday, the name of the directory i had My_Modules in last night doesn't come up right in the terminal and wont open when i put in the name as it appears or as it actually is, strange. but now i have a new directory and it works properly, thank you for your time.

seems i was mistaken.. i just restarted python and tried again, didn't work again?! i opened the module file and ran it, then tried import and it worked, but this is obviously not going to be of much use. i dont understand why it wont work.

It's impossible, the PYTHONPATH must work. Can you post your ~/.bashrc ?

Gribouillis 1,391 Programming Explorer Team Colleague

yes using a copy solves the problem thank you but I have couple of questions about the second method.
If I understood correctly

s[:] = (x for x in s if x != 'r')

means reconstruct the list s from the components of the list s itself (interating through them ) but skip the element 'r' right?
Also why can't we use just s instead of s[:].

s=(x for x in s if x!= 'r')
>>> s
<generator object <genexpr> at 0xa80ff54>

If you want to use s, write

s = [x for x in s if x!= 'r']
# or
s = list(x for x in s if x!= 'r')

there is a small difference with s[:] because this code create a new list for the name s. With s[:] it only fills the same list object with new elements. For example try this

a = ['b','a','r','r','i','s','t','e','r']
b = a
c = a
b = [x for x in b if x != 'r']
print a
c[:] = [x for x in c if x != 'r']
print a

print id(a), id(b), id(c)

If you want to learn more about generator expressions, read about generators and the iterator protocol in the python documentation, but you can postpone this a little if you are new to python.

Gribouillis 1,391 Programming Explorer Team Colleague

'r' was encountered only twice, add a few prints to see what happens

s = ['b','a','r','r','i','s','t','e','r']
for x in s:
    print x,
    if(x == 'r'):
        print "\n", s, "-->",
        s.remove(x)
        print s

print "Second version: "
s = ['b','a','r','r','i','s','t','e','r']
for x in list(s):
    print x,
    if(x == 'r'):
        print "\n", s, "-->",
        s.remove(x)
        print s

""" my output-->
b a r 
['b', 'a', 'r', 'r', 'i', 's', 't', 'e', 'r'] --> ['b', 'a', 'r', 'i', 's', 't', 'e', 'r']
i s t e r 
['b', 'a', 'r', 'i', 's', 't', 'e', 'r'] --> ['b', 'a', 'i', 's', 't', 'e', 'r']
Second version: 
b a r 
['b', 'a', 'r', 'r', 'i', 's', 't', 'e', 'r'] --> ['b', 'a', 'r', 'i', 's', 't', 'e', 'r']
r 
['b', 'a', 'r', 'i', 's', 't', 'e', 'r'] --> ['b', 'a', 'i', 's', 't', 'e', 'r']
i s t e r 
['b', 'a', 'i', 's', 't', 'e', 'r'] --> ['b', 'a', 'i', 's', 't', 'e']
"""

Conclusion: don't modify the size of a list while iterating on this list, use a copy if necessary. Also the standard way to do it is

s = ['b','a','r','r','i','s','t','e','r']
s[:] = (x for x in s if x != 'r')
Gribouillis 1,391 Programming Explorer Team Colleague

You may need to restart your session. If you want, you can post a part of your .bashrc to see if it's OK.

Gribouillis 1,391 Programming Explorer Team Colleague

how do i open bashrc? i tried to in the terminal but it said permission denied?!

if your desktop is gnome, type

cd
gedit .bashrc

in the terminal. If your desktop is KDE, type

cd
kwrite .bashrc
Gribouillis 1,391 Programming Explorer Team Colleague

im sorry i dont understand, could you explain, then i will try it again?

If you are in linux, your home directory contains a file named '.bashrc' (notice the dot). Type ls ~/.bashrc or ls -a ~ in a terminal to check. Then open this file with your favorite editor and add the line

export PYTHONPATH="/path/to/your/directory/MyModules"

then save the file and exit the editor, and type 'bash' in your terminal to restart the shell. If your python IDE is opened, you need to restart it as well.

Gribouillis 1,391 Programming Explorer Team Colleague

Add the following line to your file ~/.bashrc

export PYTHONPATH="$HOME/MyModules:$HOME/fooo/bar/baz/qux/someotherdirifyouwant"

then restart a bash shell (or a terminal). You don't need an __init__.py in MyModules because it is not a python package.

Gribouillis 1,391 Programming Explorer Team Colleague

I suggest

def touch(filename):
    os.close(os.open(filename, os.O_WRONLY | os.O_CREAT, int("666", 8)))
    os.utime(filename, None)

def load(filename = 'Database.csv'):
    if not os.access(filename, os.F_OK):
        touch(filename)
    with open(filename, 'rb') as ifh:
        return list(list(row) for row in csv.reader(ifh))

Also, for your safety, indent python code with 4 spaces, no tab characters, and avoid module commands which is deprecated.

Gribouillis 1,391 Programming Explorer Team Colleague

To break the loop on empty input, use

while True:
    x_str = raw_input('Enter Start,Stop,Increment: ')
    if not x_str:
        break
    else:
        ...

edit: where is indention in your code ? Configure your editor to indent with 4 spaces when you hit the tab key.

Gribouillis 1,391 Programming Explorer Team Colleague

Perhaps you mean

>>> len('string')
6
Gribouillis 1,391 Programming Explorer Team Colleague

Okay.Need a little more help.

Is there something wrong with this?

import time
import sys
print """Enter your name
"""
myname = chr(sys.stdin.readline())
print """Hello %s. How are you today?
Would you mind entering your age?:
"""% myname
myanswer = (sys.stdin.readline())
if myanswer == "yes":
    myage = int(sys.stdin.readline())
    print "Hello %s. You are %s years old."%(myname, myage)
    time.sleep(5)
    sys.exit()
else:
    print "Suit yourself. Bye %s!"%  myname
    sys.exit(5)

Is there something wrong ? is not a good question. Good questions are

  • Does it fail with exception ? (then study the error message)
  • Does it produce the expected result ? (if not, work more)
  • Could the code be improved ? (usually yes, but it depends on your knowledge of python)

Here I suggest something like

import time
import sys
myname = raw_input("""Enter your name: """).strip()

print """Hello %s. How are you today?""" % myname

myanswer = raw_input("Would you mind entering your age? [yes] ").strip().lower()
if myanswer in ('', 'y', 'yes'):
    myage = raw_input()
    while True:
        try:
            myage = int(myage.strip())
            break
        except ValueError:
            myage = raw_input("Please enter a numeric age: ")
    print "Hello %s. You are %s years old." % (myname, myage)
    time.sleep(5)
    sys.exit()
else:
    print "Suit yourself. Bye %s!"%  myname
    sys.exit(5)
Gribouillis 1,391 Programming Explorer Team Colleague

This sounds like a bad idea. Non-portable, and it means all your Python scripts have to end in .py (and if by chance you were to have an actual binary end in .py you wouldn't be able to use it). Is there some problem with using a shebang line that this approach is trying to solve?

It's only a practical configuration of your linux system. It doesn't prevent you from adding a shebang line if you want. I've been using this trick for some time now and it is very nice. Why not use all the tools that come with a linux system ?

Gribouillis 1,391 Programming Explorer Team Colleague

It's very simple. First python finds modules installed in some site-packages directories. For example /usr/lib/python2.7/site-packages or /usr/lib64/python2.7/site-packages if you have a 64 bits python, or your per-user site-packages directory ~/.local/lib/python2.7/site-packages (you can create it yourself if it does not exist). If you want python to find modules in other directories, for example ~/foo/bar and ~/baz/taz, you can add the following line to your ~/.bashrc

export PYTHONPATH="$HOME/foo/bar:$HOME/baz/taz:$HOME/.local/lib/python2.7/site-packages:/usr/local/lib/python2.7/site-packages"

You can add as many directories as you need. Then restart a bash shell and import module sc if sc.py is in one of these directories.

@valorien: also read this as an alternative to the shebang line: http://www.daniweb.com/software-development/python/code/241988

valorien commented: very informative +1
Gribouillis 1,391 Programming Explorer Team Colleague

I think arguments to logchoose() should be swapped

from scipy import exp, special

def logchoose(n, k):
        lgn = special.gammaln(n+1)
        lgk = special.gammaln(k+1)
        lgnk = special.gammaln(n-k+1)
        return lgn - (lgnk + lgk)


def hypgeo(x, r, b, n):
    u = logchoose(x, r)
    v = logchoose(n-x, b)
    w = logchoose(n, r + b)
    return exp(u + v - w)

    
b = 2450
r = 346
N = r + b
n = 5000  
x = 1000
p = hypgeo(x,r,b,n)
print p

""" my output -->
2.79971621598e-52
"""
Gribouillis 1,391 Programming Explorer Team Colleague

Here is function to find punctuation "by hand", but I won't help you more because it's homework.

text = """In 1876 the then Duke of Marlborough was appointed Viceroy and brought his son,
Lord Randolph Churchill, to reside at the Little Lodge as his Private Secretary. Lord Randolph was
accompanied by his wife, Jennie and 2 year old son, Winston. Though he only spent 4 years there
it has been claimed that the young Winston first developed his fascination with militarism from
watching the many military parades pass by the Lodge."""

def find_punctuation(s):
    positions = list()
    start = 0
    while start < len(s):
        pos = list()
        for c in (".", "!", "?"):
            p = s.find(c, start)
            if p >= start:
                pos.append(p)
        if pos:
            m = min(pos)
            positions.append(m)
            start = m + 1
        else:
            break
    return positions

print(find_punctuation(text))

"""my output -->
[159, 238, 423]
"""
Gribouillis 1,391 Programming Explorer Team Colleague

It depends on your output shell. If you are using python in a terminal, you can print in colors with this module http://pypi.python.org/pypi/colorama . On the other hand, it won't work if you are using idle or another IDE with a graphical user interface (each GUI widget has its own rules to display text in colors, and idle for example has no user API to do this).

Gribouillis 1,391 Programming Explorer Team Colleague

Wel...if you ignore all of that...what do you think about the gzip error. I don't understand it at all...

The gzip error may happen because your site sometimes sends gzipped data and sometimes uncompressed data. I suggest a function which recognizes compressed data

from urllib2 import urlopen
from gzip import GzipFile
from cStringIO import StringIO

def download(url):
    s = urlopen(url).read()
    if s[:2] == '\x1f\x8b': # assume it's gzipped data
        with GzipFile(mode='rb', fileobj=StringIO(s)) as ifh:
            s = ifh.read()
    return s

s = download('http://www.locationary.com/place/en/US/Virginia/Richmond-page28/?ACTION_TOKEN=NumericAction')
print s
jacob501 commented: Very helpful!! +1
Gribouillis 1,391 Programming Explorer Team Colleague

Sorry. I'm kind of new to all this prgramming stuff. What is a BOM and how will it help?

The BOM is the 2 first bytes of the file. It's used to detect encoding (see wikipedia). In our case, I found \x1f\x8b, and google tells me that this marks files compressed with gzip. Indeed my linux system detects a compressed file and it is able to uncompress it with gunzip. Python can do this too with module gzip. Here we go:

>>> from urllib import urlretrieve
>>> urlretrieve('http://www.locationary.com/place/en/US/North_Carolina/Raleigh/Noodles_%26_Company-p1022884996.jsp', 'myfile')
>>> import gzip
>>> data = gzip.open('myfile', 'rb').read()

!!!

Gribouillis 1,391 Programming Explorer Team Colleague

Why did it only work once??

I don't know why it worked only once. Obviously the content has a special encoding. I did this

from urllib import urlretrieve
urlretrieve('http://www.locationary.com/place/en/US/North_Carolina/Raleigh/Noodles_%26_Company-p1022884996.jsp', 'myfile.jsp')

Then when I cat myfile.jsp in a terminal it looks good, but when I load the content with python, it shows the same error. We could perhaps find a BOM at the beginning of the data.

Gribouillis 1,391 Programming Explorer Team Colleague

hehe

Gribouillis 1,391 Programming Explorer Team Colleague

Oh well...thats what my code looks like already. Daniweb just changed it a little...putting it on one line doesn't change anything for me...I still get the weird result ("&lsaquo (DOT))

Or do you mean that that link worked for you and you got the HTML from it?

I mean did you replace the %26 in the url by & ?

Gribouillis 1,391 Programming Explorer Team Colleague

I get a better result with

page = urllib2.urlopen('http://www.locationary.com/place/en/US/North_Carolina/Raleigh/Noodles_&_Company-p1022884996.jsp').read()

(I replaced %26 with &)

Gribouillis 1,391 Programming Explorer Team Colleague

Did you try print repr(self.RX_BUFFER) instead ? Your buffer may contain special characters which confuse the terminal screen.

Gribouillis 1,391 Programming Explorer Team Colleague

Or even

with open('Blueprints.txt','a') as file:
    file.write(
        "world.setBlockWithNotify(i {X}, j {Y}, k {Z}, Block.{B}.BlockID);"
        .format(X=X, Y=Y, Z=Z, B=B))
Gribouillis 1,391 Programming Explorer Team Colleague

This error happens in the assignment statement when you try to assign the wrong number of values to the wrong number of names, for example

>>> a, b, c = (1, 2)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: need more than 2 values to unpack

In your case, getInputs(), returns only 2 values, but the first line in main() tries to assign these 2 values to 3 names.

Gribouillis 1,391 Programming Explorer Team Colleague

Another way

>>> from operator import itemgetter
>>> from itertools import groupby
>>> dic = {'1': ['a', 'c'],'2': ['a', 'b', 'c'], '3': ['b']}
>>> dict((x, list(t[1] for t in group)) for (x, group) in groupby(sorted(((j, k) for k, v in dic.items() for j in v), key=itemgetter(0)), key=itemgetter(0)))
{'a': ['1', '2'], 'c': ['1', '2'], 'b': ['2', '3']}
Max_16 commented: This is superfast and exactly what I needed!! Thank you! +0
Gribouillis 1,391 Programming Explorer Team Colleague

Yes, it's the syntax for py3k's print() function. In python 2, without the import, add a , at the end of the print statement to avoid the newline.

>>> if True:
...  print "hello",
...  print "world"
... 
hello world
Gribouillis 1,391 Programming Explorer Team Colleague
from __future__ import print_function

def drawBlocks(a,b,c,d,height):
    print((a * "  " + b * " *" + c * "  " + d * " *" + "\n") * height, end='') 

def drawLetterA():
    drawBlocks(1,8,1,0,2)
    drawBlocks(0,2,6,2,2)
    drawBlocks(0,10,0,0,2)
    drawBlocks(0,2,6,2,3)

drawLetterA()

Why don't you post your output with your code ? It makes better posts. Here we must guess or run your code to see what it does.

Gribouillis 1,391 Programming Explorer Team Colleague

You can also capture the data interactively with your function add_mark(), then save the dictionary to a file and read the dictionary from this file the next time you run your program

from cPickle import dump, load
marks = {"alice" : "A+", "smith" : "A", "daemon" : "B+", "lily" : "B" }

# save dict to file
with open("marks.pkl", "wb") as fout:
    dump(marks, fout)
# read dict from saved file
with open("marks.pkl", "rb") as fin:
    foo = load(fin)
print foo # prints {'smith': 'A', 'daemon': 'B+', 'lily': 'B', 'alice': 'A+'}
Gribouillis 1,391 Programming Explorer Team Colleague

Im pretty new to programming and Python. So could you perhaps explain some what the code exactly does?

Thanks.

Ok, here is the same code with some comments

# import the 'path' class from the module I posted, which you saved as
# whatever.py (but you can choose another name)
from whatever import path
# path("~").hard creates a path to your home folder. Print it to see what it does
# for me it's: path('/home/eric')
# then dividing by "foo" gives me path('/home/eric/foo')
# dividing again by "bar" gives me path('/home/eric/foo/bar')
# the path will be ok independently of your os
pathA = path("~").hard/"foo"/"bar"
pathB = path("~").hard/"hello"/"world"
# this for loop iterates over the files in the folder pathA
# for me, src takes the values
# /home/eric/foo/bar/world.png, /home/eric/foo/bar/hello.txt, etc
for src in pathA.listdir():
    if src.ext != ".txt": # skip files without extension .txt
        continue
    dst = pathB/src.name # this is /home/eric/hello/world/world.png for example
    with open(dst, "w") as ofh: # open file dst for writing
        with open(src) as lines: # open file src for reading
            for line in lines:
                # get some info
                ofh.write("something") # write to dst file
Gribouillis 1,391 Programming Explorer Team Colleague

For almost all my path manipulations, I use a module with a path class. This path class was originaly written by developpers of the python language, I added a few useful features. Here is the attached module. Save it as whatever.py and use it. Here is an example:

from whatever import path
pathA = path("~").hard/"foo"/"bar"
pathB = path("~").hard/"hello"/"world"
for src in pathA.listdir():
    if src.ext != ".txt":
        continue
    dst = pathB/src.name
    with open(dst) as ofh:
        with open(src) as lines:
            for line in lines:
                # get some info
                ofh.write("something")

The module contains many many features plus a few unix things and a few personal things, but it should run very well for most things and save you a lot of time with file system paths.

Gribouillis 1,391 Programming Explorer Team Colleague

I would first parse the lines with adapted datatypes like this

from collections import namedtuple
from itertools import islice
import re
import sys

file1 = iter("""#file  R       plp   trace  class   info
20\tQRT43\t1413\t29\tFIL\tNS=3,DP=14,Bgc=0.5,DB,H2,GT:GQ:DP:HQ 0|0:48:1:51,51 1|0:48:8:51,51 1/1:43:5
30\tQZEE43\t1413\t29\tFIL\tNS=3,DP=14,Bgc=0.2,DB,H2,GT:GQ:DP:HQ 0|0:48:1:51,51 1|0:48:8:51,51 1/1:43:5
15\tZZT43\t1413\t29\tFIL\tNS=3,DP=14,Bgd=0.7,DB,H2,GT:GQ:DP:HQ 0|0:48:1:51,51 1|0:48:8:51,51 1/1:43:5
""".strip().splitlines()) # replace this with your opened file 1

file2 = iter("""foo\tbar\tR\tbaz
13\tABG1324\tQRT43\t23455
13\tABG1324\tQRR43\t23887
13\tABG1324\tORT47\t20993
13\tABG1324\tQR453\t23455
13\tABG1324\tQROR3\t27466
""".strip().splitlines()) # replace this with your opened file 2

# Define a custom datatype to read file1

class LineA(namedtuple("LineA", "file R plp trace klass info")):    
    @property
    def bgc(self):
        return self._find_float("Bgc")
    
    @property
    def bgd(self):
        return self._find_float("Bgd")
    
    def _find_float(self, name):
        match = re.search(r"%s=([^,]*)" % name, self.info)
        if match:
            try:
                return float(match.group(1))
            except ValueError:
                return 0.0
        else:
            return 0.0

F1 = []
for line in islice(file1, 1, None):
    line = LineA(*line.split("\t", 5))
    F1.append(line)

# F1 is now a list of LineA objects, which have
# attributes .file, .R, .plp, .klass, .info, .bgc, .bgd
# the 2 last attributes are floats, with value 0.0 if nothing was found

for line in F1:
    print line
    
for line in F1:
    print line.bgc, line.bgd
    
    
LineB = namedtuple("LineB", "foo bar R baz")

F2 = []
for line in islice(file2, 1, None):
    F2.append(LineB(*line.split("\t", 3)))

# F2 is now a list of LineB objects, which have
# attributes .foo, .bar, .R, .baz

for line in F2:
    print line

# Now try to extract the information you want from these two lists F1 and F2

"""My output -->
LineA(file='20', R='QRT43', plp='1413', trace='29', klass='FIL', info='NS=3,DP=14,Bgc=0.5,DB,H2,GT:GQ:DP:HQ 0|0:48:1:51,51 1|0:48:8:51,51 1/1:43:5')
LineA(file='30', R='QZEE43', plp='1413', …
TrustyTony commented: inherit from namedtuple as factory, interesting +13
Gribouillis 1,391 Programming Explorer Team Colleague

It's a piece of cake for itertools

from itertools import islice, product, chain
import string

print ''.join(tuple(islice(chain.from_iterable(product(string.ascii_uppercase, string.ascii_lowercase, string.digits)), 79)))

""" my output -->
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9Ab0Ab1Ab2Ab3Ab4Ab5Ab6Ab7Ab8Ab9Ac0Ac1Ac2Ac3Ac4Ac5A
"""

Notice that the maximum length is 20280 and this code will not complain if you replace 79 with 25000 but it will return a string of length 20280. An alternative is to store the 20 KB string somewhere and slice it directly: pattern20KB[:79]

debasishgang7 commented: Thanks Man.. +3
Gribouillis 1,391 Programming Explorer Team Colleague

This may help

>>> import string
>>> print "Aa" + "Aa".join(string.digits)
Aa0Aa1Aa2Aa3Aa4Aa5Aa6Aa7Aa8Aa9
Gribouillis 1,391 Programming Explorer Team Colleague

prob=[start:end]

This is meaningless, but you can write

>>> prob = slice(3, 7)
>>> L = list(i**i for i in range(10))
>>> L[prob]
[27, 256, 3125, 46656]

I doubt this use of slicing was the OP's intention.

@sofia85 The problem (and this may be why your code is so unclear) is that crucial information is missing in your problem: do R values appear more than once in each file ? Do some of them appear in one file and not the other ? Before writing code, you should be able to write an algorithm which describes precisely the successive steps of your program: write pseudo-code. We still don't know what your program is supposed to do.

Gribouillis 1,391 Programming Explorer Team Colleague

Make a string with ' '.join(thelist) (or '\t'.join(), or 'foo'.join()). The list must contain strings. There are other methods, you can write the list items one by one in the file.

Gribouillis 1,391 Programming Explorer Team Colleague

In principle, you can inspect the static members, either with dir(Test) or inspect.getmembers(Test), but you can't inspect instance attributes because they are usually created by __init__(). If the Test class, and/or classes in its mro implement __slots__ attributes, you can have some instances attribute names.

Gribouillis 1,391 Programming Explorer Team Colleague

1) QUOTE_NONNUMERIC doesn't work in the reader because your input file contains unquoted non numeric fields.
2) writerow() expects a sequence and separates its items with the delimiter. If you give it a string, it will separate single characters. The correct way to use it is to pass a list or a tuple containing your row items

def main(opts, args):
    reader = csv.reader(open('input.csv', 'rb'), delimiter=',')
    csv_out = csv.writer(open('output.csv', 'w'), delimiter=',')

    for row in reader:
        content = list(row[i] for i in (0,1,5,6))
    	csv_out.writerow( content )
Gribouillis 1,391 Programming Explorer Team Colleague

The way you wrote your command, an argument shell = True is necessary in Popen(). See also this snippet http://www.daniweb.com/software-development/python/code/257449

Gribouillis 1,391 Programming Explorer Team Colleague

Sure

data = tuple(ny.loadtxt(filename) for filename in ("data1.txt", "data2.txt"))
    data += (data[0] + data[1],)
    result = ny.column_stack(data)
Gribouillis 1,391 Programming Explorer Team Colleague

I found a faster way than zip (a numpyish way)

data = tuple(ny.loadtxt(filename) for filename in ("data1.txt", "data2.txt"))
    result = ny.column_stack(data)
    print result
    print result.shape

""" my output -->
[[  45.56564    45.56564 ]
 [ 564.8484    564.8484  ]
 [ 114.25477   114.25477 ]
 ..., 
 [ 114.25477   114.25477 ]
 [   1.325588    1.325588]
 [   2.36547     2.36547 ]]
(1000, 2)
"""

the ny.column_stack(data) is 166 times faster than array(zip(*data)), with 0.015 milliseconds.

Gribouillis 1,391 Programming Explorer Team Colleague

yes, it doesn't work. I still get error message.

Does

print ny.loadtxt("data1.txt")

print a numpy array, as it should ? Please post code (also make sure to read the documentation for loadtxt())
By the way ny.array(zip(*data)) takes 2.49 milliseconds on my computer with python 2.6 and 2 arrays of size 1000.
Here are my files data1.txt and data2.txt

Gribouillis 1,391 Programming Explorer Team Colleague

Do I need a for loop? I get error TypeError: zip argument #1 must support iteration

Did you write the same code that I wrote above ? because for me it runs without errors.

Gribouillis 1,391 Programming Explorer Team Colleague

Actually its like 1000 lines, but maybe it still works for that purpose.

It should be OK. If you want to be sure, learn to use module timeit (but don't read the file 1000000 times, it's not good for your hard disk).