Basically, I have to open a CSV report (about 30,000 lines) and rename the ARTIST and TITLE if they appear in a second CSV file (about 10,000 lines) of corrected ARTIST and TITLE.
The code I came up with will scan all 31,400 lines, but for some reason, it will only replace the first instance it finds.
Here is my code:
def convert(): # StackOverflow refuses to display the indents correctly
global modified
print "\n\nConverting: " + logfile + "\n\n"
songCount = 0 # Number of lines required to be reported
unclaimedCount = 0 # Number of lines not required to be reported (used to double check accuracy or report)
freport = open(musicreportname, "w") # This is the new report we will create
flogfile = open(logfile, "r") # This is the existing report
freplacefile = open(replacefile, "r")# This file contains corrected names to be substituted and ISRC Codes
freport.write("^NAME_OF_SERVICE^|^TRANSMISSION_CATEGORY^|^FEATURED_ARTIST^|^SOUND_RECORDING_TITLE^|^ISRC^|^ALBUM_TITLE^|^MARKETING_LABEL^|^ACTUAL_TOTAL_PERFORMANCES^\n")
lineCount = 0
rlinecount = 0
for line in csv.reader(flogfile, quotechar='"', delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True):
lineCount += 1
if line[0][0] == "#":
continue
if line[16] == "S":
songCount += 1
matched = "FALSE"
rlineCount = 0
for rline in csv.reader(freplacefile, delimiter=',', quoting=csv.QUOTE_ALL, skipinitialspace=True):
rlineCount += 1
if line[3] == rline[2]:
print "Matched " + line[3]
if line[4] == rline[1]:
print "Matched " + line[3], rline[1]
output = "^" + service + "^|^" + "B" + "^|^" + rline[8] + "^|^" + rline[7] + "^|^" + rline[6] + "^|^" + line[5] + "^|^" + line[6] + "^|^" + line[2] + "^\n"
freport.write(output)
matched = "TRUE"
modified += 1
break
if matched == "FALSE":
output = "^" + service + "^|^" + "B" + "^|^" + line[3] + "^|^" + line[4] + "^|^" + line[8] + "^|^" + line[5] + "^|^" + line[6] + "^|^" + line[2] + "^\n"
freport.write(output)
else:
unclaimedCount += 1
freport.close()
flogfile.close()
freplacefile.close()
print str(songCount) + " Total Songs Found."
print "Checked " + str(lineCount) + " lines."
print "Replaced " + str(modified) + " lines."
Any help would be greatly appreciated! Thank you for looking!