简体   繁体   English

嵌套For循环在Python中读取不同的CSV文件

[英]Nested For Loops reading different CSV files in Python

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. 基本上,我必须打开一个CSV报告(大约30,000行),并重新命名ARTIST和TITLE(如果它们出现在已更正的ARTIST和TITLE的第二个CSV文件(大约10,000行)中)。

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. 我提供的代码将扫描所有31,400行,但是由于某些原因,它将仅替换找到的第一个实例。

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! 感谢您的光临!

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 – Barmar 4月27日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. 谢谢巴尔玛。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM