【发布时间】:2017-01-22 16:34:44
【问题描述】:
我想用我的 IronPython 代码中的列表设置一个文档属性。但是在添加新的文档属性时,我没有看到可用的“列表”类型。 唯一接近列表的是字符串类型 Example of a DXP that has what I want to achieve
但我无法编辑该属性以检查它是如何添加的。
【问题讨论】:
标签: ironpython spotfire
我想用我的 IronPython 代码中的列表设置一个文档属性。但是在添加新的文档属性时,我没有看到可用的“列表”类型。 唯一接近列表的是字符串类型 Example of a DXP that has what I want to achieve
但我无法编辑该属性以检查它是如何添加的。
【问题讨论】:
标签: ironpython spotfire
在您的 DXP 示例中,这只是一个带有逗号分隔值的字符串。
在您的 Python 代码中执行此操作:
my_list = ['a', 'b', 'c']
delimiter = ","
Document.Properties["MyProp"] = delimiter.join(my_list)
print Document.Property("MyProp")
>>> 'a,b,c'
稍后,当您需要对值进行迭代时,您可以轻松地将其转换回列表:
my_prop = Document.Properties["MyProp"]
delimiter = ","
my_list = my_prop.split()
print my_list
>>> ['a', 'b', 'c']
最后一点:如果您的列表包含整数或字符串以外的任何内容,则您需要以不同的方式加入它,因为 Python 对类型很挑剔:
my_list = [1, 2, 3]
delimiter = ","
Document.Properties["MyProp"] = delimiter.join(str(i) for i in my_list)
print Document.Property("MyProp")
>>> '1, 2, 3'
您可以使用int() 将其转换回整数列表:
my_prop = Document.Properties["MyProp"]
delimiter = ","
my_list = [int(i) for i in my_prop.split()]
print my_list
>>> [1, 2, 3]
【讨论】: