0

I have read other answers to similar questions on google and on this site, and none of them work in my script.

I need to sort information in a csv_file by the third column from a py script. And then, with the sorted info, find duplicates and remove them but add a count to the csv_file.

for ip in open("lists.txt"):
    with open("csv_file.csv", "a") as csv_file:
        csv_file.write("\r IP:" + ip.strip() + ", Count, A, B, C \r")
        for line in open("data.txt"):
            new_line = line.split()
            if "word" in new_line:
                if "word"+ip.strip() in new_line:
                    csv_file.write(ip.strip() + ", " + new_line[10].replace("word=", ", ") + new_line[12].replace("word=", ", "))
                    try:
                        csv_file.write(new_line[14].replace("word=", ", "))
                    except IndexError:
                        pass
                    csv_file.write("\r")
with open("csv_file.csv", "r") as inputfile:
    reader = csv.reader(inputfile)
    headers = next(reader)
    for row in reader:
        key = (row[0], row[1:])
        if key not in rows:
            rows[key] = row + [0,]
        rows[key][-1] += 1

I have no idea why this isn't working, and returning errors like:

TypeError: unhashable type: 'list'

Question: How do I sort by the 3rd column, remove duplicates and add a duplicate count to my csv_file through a py script?

4

1 に答える 1

1

私が間違っていなければ、「a」タグは次の行に書き込むために開きます。

with open("csv_file.csv", "a") as inputfile:

これは、読み取り用ではなく、書き込み用に開いていることを意味します。「r」または「+」のいずれかを使用する必要があります。

于 2013-08-23T16:25:43.950 に答える