简体   繁体   中英

How to correctly call variables from a txt file into my python code?

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!

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM