0

I'm trying to write a script that will open a CSV file and write rows from that file to a new CSV file based on the match criteria of a unique telephone number in column 4 of csv.csv. The phone numbers are always in column 4, and are often duplicated in the file, however the other columns are often unique, thus each row is inherently unique.

A row from the csv file I'm reading looks like this: (the TN is 9259991234)

2,PPS,2015-09-17T15:44,9259991234,9DF51758-A2BD-4F65-AAA2

I hit an error with the code below saying that '_csv.writer' is not iterable and I'm not sure how to modify my code to solve the problem.

import csv
import sys
import os

os.chdir(r'C:\pTest')

with open(r'csv.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=',')
    with open (r'new_csv.csv', 'ab') as new_f:
        writer = csv.writer(new_f, delimiter=',')
        for row in reader:
            if row[3] not in writer:
                writer.writerow(new_f)
1
  • What do you expect row[3] not in writer to do? You cannot test for membership in a writer; that data is already written. If you want to track phone numbers already processed, you need to keep a separate set() object and test against that. Commented Oct 1, 2015 at 18:54

1 Answer 1

2

Your error stems from this expression:

row[3] not in writer

You cannot test for membership against a csv.writer() object. If you wanted to track if you already have processed a phone number, use a separate set() object to track those:

with open(r'csv.csv', 'rb') as f:
    reader = csv.reader(f, delimiter=',')
    with open (r'new_csv.csv', 'ab') as new_f:
        writer = csv.writer(new_f, delimiter=',')
        seen = set()
        for row in reader:
            if row[3] not in seen:
                seen.add(row[3])
                writer.writerow(row)

Note that I also changed your writer.writerow() call; you want to write the row, not the file object.

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

1 Comment

That works perfectly, thank you! This is quite literally my first attempt at writing anything in Python, so the feedback is much appreciated.

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.