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?

6
  • you mean reader.next(), not next(reader) Commented Aug 23, 2013 at 16:23
  • How big is the csv file? Commented Aug 23, 2013 at 16:24
  • @yosukesabai: Why reader.next()? @bill: 266KB Commented Aug 23, 2013 at 16:31
  • 1
    sorry I have to take that back.... next() should be fine. opening 'a' may be the problem as the answer by Benjamin says. Commented Aug 23, 2013 at 16:38
  • Didn't you just ask this question? How is this different than the previous one? Commented Aug 23, 2013 at 16:38

1 Answer 1

1

If I'm not mistaken the "a" tag opens for writing in this line:

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

This means you're opening for writing, not for reading. You should use either "r" or "+".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.