简体   繁体   English

如何使用字符串获取值,将用户编辑的变量(在globals()之外)写入python中的csv

[英]How to write user edited variables (outside globals()) to csv in python using a string to get value

Let me quickly introduce the scenario. 让我快速介绍一下这个场景。 For a knowledge based engineering course I am to create a business jet using ParaPy (python based KBE software). 对于基于知识的工程课程,我将使用ParaPy(基于python的KBE软件)创建公务机。 The user should be able to change variables in an input file, the input file is to include units and comments. 用户应该能够更改输入文件中的变量,输入文件包含单位和注释。 The program will then create a geometry and open this in a GUI, the user is able to change said variables interactively in the GUI. 然后程序将创建一个几何体并在GUI中打开它,用户可以在GUI中以交互方式更改所述变量。 The user should than be able to save the current values to an output file that has the exact same format as the input file, so including the units and comments. 用户应该能够将当前值保存到与输入文件格式完全相同的输出文件中,因此包括单位和注释。 I'm sorry for the fact that the problem is not recreatable without the ParaPy software but I found it difficult to describe without using the entire scenario. 我很抱歉没有ParaPy软件这个问题无法恢复,但我发现很难在不使用整个场景的情况下进行描述。

I thought it would be suitable to use CSV for this. 我认为使用CSV是合适的。 The reading part works fine but I am running into trouble with the writing part. 阅读部分工作正常,但我在写作部分遇到了麻烦。 A simplified version of the case has the following code. 案例的简化版具有以下代码。 This creates a box geometry from the width, height, and length variables. 这将根据宽度,高度和长度变量创建一个框几何体。 These are the variables that can be edited in the GUI and which I need to save again. 这些是可以在GUI中编辑的变量,我需要再次保存。 However, I have no idea how to access them without hardcoding it. 但是,我不知道如何在不对其进行硬编码的情况下访问它们。 I was trying to use the input file to read the variable name and then request the value using that name (see else stamen in def save_vars(self): ). 我试图使用输入文件来读取变量名称,然后使用该名称请求值(请参阅def save_vars(self)中的 else stamen :)

from parapy.core import *
from parapy.geom import *
from csv_in_out import *

class Boxy(Base):
    # Read function from external file
    read = ReadInput(file_name='box.csv')
    # Create dict
    variables = read.read_input

    # Set the editable variables
    width = Input(variables["width"])
    height = Input(variables["height"])
    length = Input(variables["length"])

    @Part
    # Create the geometry
    def box1(self):
        return Box(width=self.width,
                   height=self.height,
                   length=self.length)

    @Attribute
    def save_vars(self):
        path = self.read.generate_path
        first_row = True
        with open(path[0], 'rb') as file:  # Open file
            reader = csv.reader(file, delimiter=',', quotechar='|')  # Read into reader and section rows and columns
            with open(path[1], 'wb') as outfile:
                filewriter = csv.writer(outfile, delimiter=',', quotechar='|')
                for row in reader:
                    if first_row == True:
                        filewriter.writerow(row)
                        first_row = False
                    else:
                        # Find the name of the variable that we want to request and save
                        var_name = row[0]

                        value = self.var_name # I know this does not work, but I basically want to get the input variables
                        # Update the value in row
                        row[1] = value
                        # Write the row to a new file
                        filewriter.writerow(row)
        return 'Saved'


if __name__ == '__main__':
    from parapy.gui import display

    obj = Boxy()
    display(obj)

Below is the csv file that I used for this. 下面是我用于此的csv文件。

var_name,Value,Unit,Comment
width,2,[m],Width of the box
height,3,[m],Height of the box
length,4,[m],Length of the box

Thank you for taking the time to look at my problem. 感谢您抽出宝贵时间来查看我的问题。 If anything is unclear or you need more information please let me know. 如果有任何不清楚或您需要更多信息,请告诉我。

As stovfl pointed out there is a nice class for writing configuration files in Python, that is definitely the best way to solve it if you are still starting from scratch. 正如stovfl指出的那样,在Python中编写配置文件有一个很好的类,如果你仍然从头开始,这绝对是解决它的最佳方法。

However, I also found how to get my thing to work. 但是,我也找到了如何使我的工作。 Where I had the following: 我在哪里有以下内容:

The line in the else statement: else语句中的行:

value = self.var_name

Should be changed to 应该改为

value = getattr(self,var_name)

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

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