简体   繁体   English

如何正确地将txt文件中的变量调用到我的python代码中?

[英]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. 我必须在文件中输入50,000个棱镜。 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. 它包含约50,000行上述数据。 What I am trying to figure out how to do is to import and run all 50,000 prisms in the one script. 我试图弄清楚该怎么做,是在一个脚本中导入并运行所有50,000个棱镜。 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? 我知道不可能将model1 ... model50000写入脚本,我只是希望周围有一个不错的方法?

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. 将此模式应用于您的问题,您会注意到您具有model1model2等。 This is the clue that we need a list. 这就是我们需要清单的线索。 In fact, the prism.potential function accepts such a list. 实际上, prism.potential函数接受这样的列表。

Also, I notice you are using numpy, so you have access to the numpy.loadtxt function, which can read the file into an array. 另外,我注意到您正在使用numpy,因此您可以访问numpy.loadtxt函数,该函数可以将文件读取到数组中。

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. 编辑:csv模块可能对此有用,特别是如果您的某些或所有字段为数字并且任何字符串字段都用引号引起来时,因为它使您可以控制如何将字段解释为类型。 Pass quoting=csv.QUOTE_NONNUMERIC to the reader to tell it that all unquoted columns are numeric: quoting = csv.QUOTE_NONNUMERIC传递给读者,以告诉它所有未引用的列都是数字:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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