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

Marcin
  • 48,559
  • 18
  • 128
  • 201
Mike
  • 4,099
  • 17
  • 61
  • 83
  • possible duplicate of [How to remove 'u'(unicode) from a dictionary item](http://stackoverflow.com/questions/13802151/how-to-remove-uunicode-from-a-dictionary-item) – Marcin Sep 18 '13 at 18:29
  • I am trying to copy and paste the resulting values into a spreadhseet, so that solution doesn't work. – Mike Sep 18 '13 at 18:33

3 Answers3

2

The issue in your code is that you are calling str(value), where value is an array. So what happens is that the array object's __str__ function is getting invoked and it has its own way of making a string representation of the underlying values. This default representation uses repr to show individual elements' values. Since in this case the array elements are unicode string, you see the 'u' in the output.

As a solution, what you want to do is to "unroll" the array manually and build up your own list representation. Here's one way of doing it:

for key, value in result.iteritems():
    print key + ": " +  ",".join(["'%s'" % v for v in value])
Dmitry B.
  • 9,107
  • 3
  • 43
  • 64
0

I believe this is what you're after

for key, value in result.iteritems():
    print key + ": " +  str([ str(v) for v in value ])
Chris Eberle
  • 47,994
  • 12
  • 82
  • 119
  • @Marcin it's a list that's being printed. It will be iterated over. Your map solution is precisely as efficient as list comprehension. – Chris Eberle Sep 18 '13 at 18:47
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))

Simplified with string formatting, and map to change each value to a string, using the default encoding.

Marcin
  • 48,559
  • 18
  • 128
  • 201
  • Although you down voted me, thanks marcin for taking the time to help me. That's exactly what i was trying to figure out. the post you offered didn't exactly answer the question I was asking, but it was my fault for not being a little more clear as to what my end result was. – Mike Sep 18 '13 at 20:16
  • @Mike Well, I hope this helps. The question does fall under a major area of confusion for a lot of programmers (not sure why), and possibly I was over hasty to identify duplication. – Marcin Sep 18 '13 at 20:21