HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Extracting two columns from exported iTunes playlist

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
exporteditunescolumnstwoplaylistextractingfrom

Problem

My friend wanted me to send her a list of songs from one of my iTunes playlists, so I exported the playlist within the app itself. It comes as a plain text file, where each field is separated a '\t' and each line is separated by a '\r'. Since there is so much extraneous information, I decided to write a Python script that would delete all fields but the song title and artist name.

How is the design and format of my code? How does it fit in with Python best practices? Is there a much easier way to do accomplish the same job that I just overlooked?

The input file looks like:

Name\tArtist\tComposer\tAlbum\tGrouping\tGenre\tSize\tTime\tDisc Number\tDisc Count\tTrack Number\tTrack Count\tYear\tDate Modified\tDate Added\tBit Rate\tSample Rate\tVolume Adjustment\tKind\tEqualizer\tComments\tPlays\tLast Played\tSkips\tLast Skipped\tMy Rating\tLocation

You Shook Me All Night Long\tAC/DC\t\tThe Very Best\t\tHard Rock\t5468228\t212\t\t\t9\t\t2001\t4/17/12, 2:29 PM\t4/17/12, 2:26 PM\t192\t44100\t\tMPEG audio file\t\t\t5\t3/12/13, 10:41 PM\t\t\t\tMacintosh HD:Users:UserName:Music:iTunes:iTunes Media:Music:AC_DC:The Very Best:09 You Shook Me All Night Long.mp3


The output file looks like:

Name\tArtist\t

You Shook Me All Night Long\tAC/DC\t

Miss Murder\tAFI\t


My code is:

from sys import argv

def main(file):
with open(argv[1], 'r') as file:
data = file.read()
newdata = data.split('\r')
output = []
for line in newdata:
tabc = 0
newline = ""
for char in line:
newline += char
if char == '\t':
tabc += 1
if tabc == 2: break
output.append(newline)
outPutString = '\n'.join(output)
with open(argv[1][:-4]+'Out.txt', 'w') as file:
file.write(outPutString)

if __name__ == '__main__':
file = argv[1]
main(file)

Solution

Simple mistake

The function main takes a filename as an argument but doesn't use it. Instead, it retrieves it from argv.

Also, filename would be a better name for a filename than file.

Context

StackExchange Code Review Q#69099, answer score: 2

Revisions (0)

No revisions yet.