본문 바로가기
파이썬

파이썬 Save results to csv file with Python

by º기록 2020. 11. 14.
반응형
import csv

with open('test.csv', 'rb') as f:
  data = list(csv.reader(f))

import collections
counter = collections.defaultdict(int)
for row in data:
    counter[row[1]] += 1
for row in data:
    if counter[row[1]] >= 4:
      writer = csv.writer(open("test1.csv", "wb"))
      writer.writerows(row)

이상한 출력이 나옵니다! 이 코드에 어떤 문제가 있습니까?

 

해결 방법

 

csv.writer 사용 :

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[0]] += 1


writer = csv.writer(open("/path/to/my/csv/file", 'w'))
for row in data:
    if counter[row[0]] >= 4:
        writer.writerow(row)

 

참조 페이지 https://stackoverflow.com/questions/3345336

 

 

반응형

댓글