[英]Formatting an output file
我目前正在使用python索引我的音乐收藏。 理想情况下,我希望输出文件的格式为:
"Artist;
Album;
Tracks - length - bitrate - md5
Artist2;
Album2;
Tracks - length - bitrate - md5"
但我似乎无法解决如何实现这一目标。 有什么建议么?
>>> 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
如果您的输入数据是一个元组列表,每个元组包含6个字符串(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)
它本质上是一样容易,如果你输入的数据是不同的格式,但除非你告诉我们, 什么样的格式输入数据是,这是非常愚蠢的为我们只是试图去猜测。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.