Python CSV DictReader/Writer issues

csv, python

Solution

I don't know either, but since all you're doing is copying lines from one file to another why are you bothering with the `csv` stuff at all? Why not something like:

f = open("my_csv_file.csv", "r")
target = open("united.csv", 'w')

f.readline()
f.readline()
for line in f:
    target.write(line)

Problem

I'm trying to extract a bunch of lines from a CSV file and write them into another, but I'm having some problems. ``` import csv f = open("my_csv_file.csv", "r") r = csv.DictReader(f, delimiter=',') fieldnames = r.fieldnames target = open("united.csv", 'w') w = csv.DictWriter(united, fieldnames=fieldnames) while True: try: row = r.next() if r.line_num <= 2: #first two rows don't matter continue else: w.writerow(row) except StopIteration: break f.close() target.close() ``` Running this, I get the following error: ``` Traceback (most recent call last): File "unify.py", line 16, in <module> w.writerow(row) File "C:\Program Files\Python25\lib\csv.py", line 12 return self.writer.writerow(self._dict_to_list(row File "C:\Program Files\Python25\lib\csv.py", line 12 if k not in self.fieldnames: TypeError: argument of type 'NoneType' is not iterable ``` Not entirely sure what I'm dong wrong.

Original source