Formatting an output file
I'm currently indexing my music collection with python. Ideally I'd like my output file to be formatted as;
"Artist;
Album;
Tracks - lengt开发者_JAVA技巧h - bitrate - md5
Artist2;
Album2;
Tracks - length - bitrate - md5"
But I can't seem to work out how to achieve this. Any suggestions?
>>> import textwrap
>>> class Album(object):
... def __init__(self, title, artist, tracks, length, bitrate, md5):
... self.title=title
... self.artist=artist
... self.tracks=tracks
... self.length=length
... self.bitrate=bitrate
... self.md5=md5
... def __str__(self):
... return textwrap.dedent("""
... %(artist)s;
... %(title)s;
... %(tracks)s - %(length)s - %(bitrate)s - %(md5)s"""%(vars(self)))
...
>>> a=Album("album title","artist name",10,52.1,"320kb/s","4d53b0cb432ec371ca93ea30b62521d9")
>>> print a
artist name;
album title;
10 - 52.1 - 320kb/s - 4d53b0cb432ec371ca93ea30b62521d9
If your input data is a list of tuples each with 6 strings (artist, album, tracks, length, bitrate, md5)
:
for artist, album, tracks, length, bitrate, md5 in input_data:
print "%s;" % artist
print "%s;" % album
print " %s - %s - %s - %s" % (tracks, length, bitrate, md5)
It's essentially just as easy if your input data is in a different format, but unless you tell us what format that input data is in, it's pretty silly for us to just try to guess.
精彩评论