tags:

views:

60

answers:

3
import csv

with open('thefile.csv', 'rb') as f:
  data = list(csv.reader(f))
  import collections
  counter = collections.defaultdict(int)

  for row in data:
        counter[row[10]] += 1


with open('/pythonwork/thefile_subset11.csv', 'w') as outfile:
    writer = csv.writer(outfile)
    sample_cutoff=500
    b[]
    for row in data:
        if counter[row[10]] >= sample_cutoff:
           writer.writerow(row)

in addition to writing the data to a file, i would like to insert it into a list b[]

can i just do b.insert[row] ?

+2  A: 

It's b.append(row), but otherwise yes. And instead of b[] you want b = []. Another way to do it would be to make the list first, and then just write each element of the list to the file:

b = [row for row in data if counter[row[10]] >= sample_cutoff]
map(writer.writerow, b)
Michael Mrozek
+1  A: 

It's list.insert(index, item) or list.append(item).

Also, b[] is a name error and syntax error. I suppose you mean b = [].

delnan
You cannot edit add or remove items from `b = ()`.
Ashish
`()` is the syntax for the empty tuple. Lists are declared with `[]`
Daenyth
Of course you're right, thanks for pointing the typo out.
delnan
+1  A: 

Yes, b = [] would support an insert method. The syntax is as follows.

b.insert(postiion_int, element)
Ashish