csv.DictWriter로 헤더 행을 작성하는 방법은 무엇입니까?
csv.DictReader
개체가 있고 CSV 파일로 작성하고 싶다고 가정 합니다. 어떻게 할 수 있습니까?
다음 과 같이 데이터 행을 쓸 수 있다는 것을 알고 있습니다 .
dr = csv.DictReader(open(f), delimiter='\t')
# process my dr object
# ...
# write out object
output = csv.DictWriter(open(f2, 'w'), delimiter='\t')
for item in dr:
output.writerow(item)
그러나 필드 이름을 어떻게 포함시킬 수 있습니까?
편집 :
2.7 / 3.2에는 새로운 writeheader()
방법이 있습니다. 또한 John Machin의 답변은 헤더 행을 작성하는 더 간단한 방법을 제공합니다. 이제 2.7 / 3.2에서 사용 가능한 방법
을 사용하는 간단한 예 writeheader()
:
from collections import OrderedDict
ordered_fieldnames = OrderedDict([('field1',None),('field2',None)])
with open(outfile,'wb') as fou:
dw = csv.DictWriter(fou, delimiter='\t', fieldnames=ordered_fieldnames)
dw.writeheader()
# continue on to write data
DictWriter를 인스턴스화하려면 fieldnames 인수가 필요합니다.
에서 문서 :
fieldnames 매개 변수는 writerow () 메소드에 전달 된 사전의 값이 csvfile에 기록되는 순서를 식별합니다.
다른 말로하면 : Python dicts는 본질적으로 순서가 지정되지 않았기 때문에 Fieldnames 인수가 필요합니다.
다음은 헤더와 데이터를 파일에 쓰는 방법의 예입니다.
참고 : with
명령문은 2.6에서 추가되었습니다. 2.5를 사용하는 경우 :from __future__ import with_statement
with open(infile,'rb') as fin:
dr = csv.DictReader(fin, delimiter='\t')
# dr.fieldnames contains values from first row of `f`.
with open(outfile,'wb') as fou:
dw = csv.DictWriter(fou, delimiter='\t', fieldnames=dr.fieldnames)
headers = {}
for n in dw.fieldnames:
headers[n] = n
dw.writerow(headers)
for row in dr:
dw.writerow(row)
@FM이 주석에서 언급했듯이 헤더 쓰기를 한 줄로 압축 할 수 있습니다. 예 :
with open(outfile,'wb') as fou:
dw = csv.DictWriter(fou, delimiter='\t', fieldnames=dr.fieldnames)
dw.writerow(dict((fn,fn) for fn in dr.fieldnames))
for row in dr:
dw.writerow(row)
몇 가지 옵션 :
(1) csv.DictWriter가 다시 목록으로 변환하고 csv.writer 인스턴스에 전달할 수 있도록 필드 이름에서 ID 매핑 (즉, 아무것도하지 않음) dict를 열심히 만듭니다.
(2) 문서에 "기본 writer
인스턴스"가 언급 되어 있으므로 사용하기 만하면됩니다 (마지막의 예).
dw.writer.writerow(dw.fieldnames)
(3) csv.Dictwriter 오버 헤드를 피하고 csv.writer로 직접 수행
데이터 쓰기 :
w.writerow([d[k] for k in fieldnames])
또는
w.writerow([d.get(k, restval) for k in fieldnames])
Instead of the extrasaction
"functionality", I'd prefer to code it myself; that way you can report ALL "extras" with the keys and values, not just the first extra key. What is a real nuisance with DictWriter is that if you've verified the keys yourself as each dict was being built, you need to remember to use extrasaction='ignore' otherwise it's going to SLOWLY (fieldnames is a list) repeat the check:
wrong_fields = [k for k in rowdict if k not in self.fieldnames]
============
>>> f = open('csvtest.csv', 'wb')
>>> import csv
>>> fns = 'foo bar zot'.split()
>>> dw = csv.DictWriter(f, fns, restval='Huh?')
# dw.writefieldnames(fns) -- no such animal
>>> dw.writerow(fns) # no such luck, it can't imagine what to do with a list
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\python26\lib\csv.py", line 144, in writerow
return self.writer.writerow(self._dict_to_list(rowdict))
File "C:\python26\lib\csv.py", line 141, in _dict_to_list
return [rowdict.get(key, self.restval) for key in self.fieldnames]
AttributeError: 'list' object has no attribute 'get'
>>> dir(dw)
['__doc__', '__init__', '__module__', '_dict_to_list', 'extrasaction', 'fieldnam
es', 'restval', 'writer', 'writerow', 'writerows']
# eureka
>>> dw.writer.writerow(dw.fieldnames)
>>> dw.writerow({'foo':'oof'})
>>> f.close()
>>> open('csvtest.csv', 'rb').read()
'foo,bar,zot\r\noof,Huh?,Huh?\r\n'
>>>
Another way to do this would be to add before adding lines in your output, the following line :
output.writerow(dict(zip(dr.fieldnames, dr.fieldnames)))
The zip would return a list of doublet containing the same value. This list could be used to initiate a dictionary.
참고URL : https://stackoverflow.com/questions/2982023/how-to-write-header-row-with-csv-dictwriter
'Programming' 카테고리의 다른 글
C ++ 맵 액세스가 한정자를 버림 (const) (0) | 2020.08.13 |
---|---|
mysql-얼마나 많은 열이 너무 많습니까? (0) | 2020.08.13 |
이블 모드 모범 사례? (0) | 2020.08.13 |
DLL을로드 할 수 없습니다 (모듈을 찾을 수 없음 HRESULT : 0x8007007E). (0) | 2020.08.13 |
gulp globbing- 디렉토리 아래의 모든 것을 보는 방법 (0) | 2020.08.13 |