0

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!

L Purloi
  • 1
  • 1
  • 1
    Myself and another user fixed the code formatting to be a little more clear - can you confirm we didn't screw the indentation up? For future reference, if you add four spaces before code it'll be placed into a code block and be easier to read. – thegrinner Apr 27 '16 at 18:05
  • Nested loops is the wrong way to do this, because it means it has to read the second file 30,000 times. Read the second file once and create a dictionary with all the mappings. Then read the first file and perform all the renames using the mapping dictionary. – Barmar Apr 27 '16 at 18:12
  • I am still trying to edit this in a way it shows correct indentation... – L Purloi Apr 27 '16 at 18:25
  • I will try your approach Barmar. Is your approach more "Pythonic"? More Effecient? Right now, it takes just 18 seconds, and only needs to run the first of every month. Still, I want to learn "The Right Way". Thank You. – L Purloi Apr 27 '16 at 18:39
  • I wonder if you need to tell it to start back at the beginning of the freplacefile. http://stackoverflow.com/questions/431752/python-csv-reader-how-do-i-return-to-the-top-of-the-file – jcfollower Apr 27 '16 at 20:43

1 Answers1

0

Read the second file once and create a dictionary with all the mappings. Then read the first file and perform all the renames using the mapping dictionary. – Barmar Apr 27 at 18:12

I followed Barmar's advice. I just started over. I used tuple instead of dict, but same idea. I never did find the error in the above code, but everything now works as expected. Thanks Barmar.

L Purloi
  • 1
  • 1