0

I've built a python dictionary as follows:

result = {}
for fc in arcpy.ListFeatureClasses():
    for field in arcpy.ListFields(fc):
        result.setdefault(field.name, []).append(fc)

which takes the name of the fields in each table (feature class) and sets tyhem as the key value in the dictionary and goes on to set the name of the table as the value. This works fine because my goal is to find out which tables have the same fields. I can go on to iter over the items and print out the key, value pairs:

for key, value in result.iteritems():
    print key + ": " +  str(value)

which returns:

COMMENTS: [u'TM_FC', u'GT_FC', u'HG_FC', u'PO_FC', u'FU_FC']

I want to print out the key values as a string value instead of the unicode stuff so that it looks like this:

COMMENTS: 'TM_FC', 'GT_FC', 'HG_FC', 'PO_FC', 'FU_FC'

I've been playing around with the 'str' function and various other ways to format and convert to string, but I'm always returning the same original value list. Can anyone suggest a way to accomplish what I'm looking for?

Thanks, Mike

4

3 回答 3

2

您的代码中的问题是您正在调用 str(value),其中 value 是一个数组。所以发生的事情是数组对象的__str__函数被调用,它有自己的方式来制作底层值的字符串表示。此默认表示用于repr显示单个元素的值。由于在这种情况下,数组元素是 unicode 字符串,因此您会在输出中看到“u”。

作为一种解决方案,您要做的是手动“展开”数组并建立自己的列表表示。这是一种方法:

for key, value in result.iteritems():
    print key + ": " +  ",".join(["'%s'" % v for v in value])
于 2013-09-18T18:33:54.470 回答
0

我相信这就是你所追求的

for key, value in result.iteritems():
    print key + ": " +  str([ str(v) for v in value ])
于 2013-09-18T18:32:03.293 回答
0
result = {u'Comments':[u'TM_FC', u'GT_FC', u'HG_FC', u'PO_FC', u'FU_FC']}
for k,v in result.iteritems():
    print u'%s:%s' % (k,map(unicode.encode,v))

通过字符串格式进行简化,并将map每个值更改为字符串,使用默认编码。

于 2013-09-18T18:35:32.170 回答