Hi, I have 2 columns of data, separated by a space, which I am trying to import into 2 variables as a tuple. Each column as a variable.
I know the program would involve something using line.split() or line.strip() but I can't figure out how I would do it.

Also I was wondering if anyone knows a good way of pausing a program while it is running. My program runs in a terminal within a while loop. Is there some sort of clause in which i could pause and resume it with the space bar?
Thanks
Daniel

Don't know about the pausing, but:

print tuple("Foo bar".split(" "))
#Output: ("Foo", "bar",)

That's an easy way. If the lines would contain more than one space between values, then some regex is in order

import re
reObj = re.compile('\s+')

for line in f.readlines():
    print tuple(reObj.split(line))

You could use the python debugger to put a break statement I guess? I'm not terribly versed in using it, but from my limited exposure I know that you can put break statements and step through loops, etc. just like most other debuggers. In order to use it you have to import pdb into your program... Here's some docs

This sounds like a problem for csv. This example will read a file with two columns separated by a space. Also note that the items themselves cannot contain spaces unless you also specify an escape character, which you can read more about in the link below.

import csv
reader = csv.reader(open('FILENAME'), delimiter=' ')
for row in reader:
    item1, item2 = row

The item1, item2 = row part could also be written:

item1 = row[0]
item2 = row[1]

Here's a reference for the csv module:
http://docs.python.org/library/csv.html

For the pause part, raw_input('Press enter to continue...') would work, unless you insist on hitting the spacebar.

Thankyou for showing me the csv module. It looks like it will save a lot of time in the future!
I am having a problem though. I am simulating the motion of particles in a fluid using python. The particles are created initially in a square lattice shape using the visual modules. This is done like this:

for x in range(0,nlat):

    for y in range(0,nlat):

        disk=sphere(pos=(x*s,y*s), radius = r, color=color.red)

        alldisks.append(disk)

        disk.id = (x,y)

I would like to resume the simulation again after it has been disturbed by putting the x and y values to what they were when they were outputted.

I have naively tried to do it by putting the following after the code above. But it segfaults every time.

reader=csv.reader(open("xypos22800000.0"), delimiter=" ")
xreslist=[]
yreslist=[]
for row in reader:
    xres, yres = row
    xreslist.append(xres)
    yreslist.append(yres)
for disk in alldisks:
    disk.x=xreslist
    disk.y=yreslist

Regarding the pausing, I wouldn't mind using enter, but I would need to be able to pause and resume arbitrarily, as an interrupt rather than being prompted to continue. I realise now that I'm probably in way over my head with that one!

Shouldn't this:

for disk in alldisks:
    disk.x=xreslist
    disk.y=yreslist

be something like this:

for x in len(alldisks):
    alldisks[x].x=xreslist[x]
    alldisks[x].y=yreslist[x]

I get an error with that:

Traceback (most recent call last):
File "resumesimulation.py", line 118, in <module>
for x in len(alldisks):
TypeError: 'int' object is not iterable

Ah, yes. Make that:

for x in range(alldisks):

Also note that we are assuming alldisks, xreslist and yreslist are the same length.

I got an error with the first one.
for x in range(alldisks):
TypeError: range() integer end argument expected, got list.
so I tried range(len(alldisks))
and got a different error:
disk[x].x=xreslist[x]
TypeError: 'sphere' object is unindexable
I'm not sure how to get at the x and y values of the object! Damn the visual module!

What about this:

for x in range(len(alldisks)):
    alldisks[x].id = (xreslist[x], yreslist[x])

Still not working.
I don't get an error this time but outputting the x,y positions after this shows that it hasn't worked, they are still in their initial square configuration

Is there anyway I could create the disks myself without using the visual module(visualisation is not important at this stage)
I'm not too good with object orientated stuff yet, but could I create them like:

class disks:
    def __init__(self):
        self.radius=0.5
        self.x
        self.y

how could I implement this?

This article has been dead for over six months. Start a new discussion instead.