0

I have a list that is taken from id3. tags from an .mp3. When i print the list, its a mixture of strings and tuples with unicode.

I get the following from a straight print out of the list.

[('album', [u'Singles']), ('artist', [u'Led Zeppelin']), ('title', [u'Kashmir'])]

I want to have this:

album Singles, artist Led Zeppelin, title Kashmir

I have tried to use the following:

nvar = filter(lambda x: x!="[u'" and x!="(" and x!=")" and x!="]", var)

which prints:

[('album', [u'Singles']), ('artist', [u'Led Zeppelin']), ('title', [u'Kashmir'])]

I can use:

>>> nvar = filter(lambda x: x!="[" and x!="'" and x!="(" and x!=")" and x!="]", str(var))
>>> nvar
'album, uSingles, artist, uLed Zeppelin, title, uKashmir'

but I am left with the u from unicode. If I use x!="u", then any word with a u in it will wrong after that function runs.

Also this seems to be the hard way to do things.

Any ideas? Thanks in advance.

4

2 に答える 2

1

tagsこれが実際に (str, list) の 2 タプルを含む Python リストであると仮定すると、次のようになります。

tags = [('album', [u'Singles']), ('artist', [u'Led Zeppelin']), ('title', [u'Kashmir'])]
print ', '.join('{} {}'.format(el[0], el[1][0]) for el in tags)
# album Singles, artist Led Zeppelin, title Kashmir
于 2013-07-20T19:31:09.227 に答える
0

以下のようなものが動作するはずです。「u」は「(」と「」の間にあるため、「実際の」データには影響しません。

    def Plain(self, U_String) :
          P_String = str(U_String)
          m=re.search("^\(\u?\'(.*)\'\,\)$", P_String)
          if (m) :  #Typical unicode
             P_String = m.group(1).decode("utf8")
          return P_String  
于 2014-04-11T21:36:22.023 に答える