0

The pertinent code runs as follows:

model = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
data = np.array([prism.potential(x, y, z, [model])

I have to input ~50,000 prisms into the file. Simple testing has determined that a format which works is as follows:

model1 = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
model2 = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
model3 = Prism(x1, x2, y1, y2, z1, z2, {'density': ρ})
data = np.array([prism.potential(x, y, z, [model1, model2, model3])

The file I need to import the prisms from is formatted such as:

x1 x2 y1 y2 z1 z2 ρ

It contains ~50,000 lines of said data. What I am trying to figure out how to do is to import and run all 50,000 prisms in the one script. I know that it is not possible to write model1 ... model50000 into the script, I am just hoping there is a nice way around it?

Thanks in advance!

7
  • Figure out how to convert one line, and then use a loop to do it for all the input lines Commented Dec 9, 2014 at 4:45
  • I'm confused what your asking. Do you want to import it to run like part of your code? Or you you want to read the data into Python? Commented Dec 9, 2014 at 4:45
  • @Torkoal I am still quite inexperienced with coding. I am not even sure how to answer your question. All I want to do is figure out how to run my script, whichever method it takes. Commented Dec 9, 2014 at 4:49
  • @MarkkuK. Thanks. I am still rather inexperienced and don't know how to do even that. Commented Dec 9, 2014 at 4:50
  • You could store each entry into a list model and then access each individual one by indexing into the list. Commented Dec 9, 2014 at 5:03

2 Answers 2

3

As a general rule, if you encounter code like this, where similar variables with number endings are being used:

a1 = 4
a2 = 2
a3 = 5

You can replace these variables with a list which you can either build at once:

a = [4, 2, 5]

or build incrementally

a = []
a.append(4)
a.append(2)
a.append(5)

Applying this pattern to your problem, you'll notice you have model1, model2 and so on. This is the clue that we need a list. In fact, the prism.potential function accepts such a list.

Also, I notice you are using numpy, so you have access to the numpy.loadtxt function, which can read the file into an array.

So,

# Read the file into an array
filedata = np.loadtxt('filename')

# Build a list with all the Prisms
models = []
for x1, x2, y1, y2, z1, z2, ρ in filedata:
    models.append(Prism(x1, x2, y1, y2, z1, z2, {'density': ρ}))

Now we have a list of models which we can pass on the other functions. You'll have to be more explicit about how that part works.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you for an easy to follow explanation!
1

you could loop through each line in the file:

with open(fname) as f:
    content = f.readlines()
    for line in content: 
        (x1, x2, y1, y2, z1, z2, p) = line.split()  # for space separated fields
        # do something

that will read the fields in as strings, which you may need them cast to numbers, so for that as per the answer here you could do things like:

>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545

Edit: the csv module might good for this, especially if some or all of your fields are numeric and any string fields are surrounded by quotes, because it gives you some control over how fields are interpreted as a type. Pass quoting=csv.QUOTE_NONNUMERIC to the reader to tell it that all unquoted columns are numeric:

import csv
with open('data.txt') as csvfile:
    reader = csv.reader(csvfile, delimiter=' ', quoting=csv.QUOTE_NONNUMERIC)
    for row in reader:
        # row is a list that contains your values

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.