Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #88369
| Path | csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4a.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail |
|---|---|
| Return-Path | <python-python-list@m.gmane.org> |
| X-Original-To | python-list@python.org |
| Delivered-To | python-list@mail.python.org |
| X-Spam-Status | OK 0.003 |
| X-Spam-Evidence | '*H*': 0.99; '*S*': 0.00; 'else:': 0.03; 'output': 0.05; '(python': 0.07; 'think,': 0.07; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'record.': 0.09; 'subject:extra': 0.09; 'url:activestate': 0.09; "'w')": 0.16; 'csv': 0.16; 'dict': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'values:': 0.16; 'wrote:': 0.18; 'header:User-Agent:1': 0.23; '31,': 0.24; 'pass': 0.26; 'values': 0.27; 'header:X -Complaints-To:1': 0.27; 'url:code': 0.29; "i'm": 0.30; "skip:' 10": 0.31; '25,': 0.31; 'file': 0.32; 'stuff': 0.32; 'another': 0.32; '(e.g.': 0.33; 'skip:t 40': 0.33; 'skip:d 20': 0.34; "i'd": 0.34; 'problem': 0.35; 'connection': 0.35; 'case,': 0.35; 'but': 0.35; 'add': 0.35; 'there': 0.35; 'hi,': 0.36; 'should': 0.36; 'connections': 0.38; 'skip:[ 10': 0.38; 'to:addr:python-list': 0.38; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'field': 0.63; '30,': 0.65; 'obvious': 0.74; 'dict,': 0.84; 'dict.': 0.84; 'here)': 0.84; 'subject:Using': 0.84; 'victor': 0.84; 'subject:add': 0.91; 'connection,': 0.95 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| To | python-list@python.org |
| From | Peter Otten <__peter__@web.de> |
| Subject | Re: Using csv DictWriter - add a extra field |
| Date | Tue, 31 Mar 2015 10:22:46 +0200 |
| Organization | None |
| References | <a72cf80c-cb27-4ec2-bcf4-76f3b208a6d4@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Gmane-NNTP-Posting-Host | p57bd93cd.dip0.t-ipconnect.de |
| User-Agent | KNode/4.13.3 |
| X-BeenThere | python-list@python.org |
| X-Mailman-Version | 2.1.19 |
| Precedence | list |
| List-Id | General discussion list for the Python programming language <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.363.1427790193.10327.python-list@python.org> (permalink) |
| Lines | 52 |
| NNTP-Posting-Host | 2001:888:2000:d::a6 |
| X-Trace | 1427790193 news.xs4all.nl 2880 [2001:888:2000:d::a6]:34580 |
| X-Complaints-To | abuse@xs4all.nl |
| Xref | csiph.com comp.lang.python:88369 |
Show key headers only | View raw
Victor Hooi wrote:
> Hi,
>
> I have a dict named "connections", with items like the following:
>
> In [18]: connections
> Out[18]:
> {'3424234': {'end_timestamp': datetime.datetime(2015, 3, 25, 5, 31, 30,
> {406000, tzinfo=datetime.timezone(datetime.timedelta(-1, 61200))),
> 'ip_address': '10.168.8.36:52440',
> 'open_timestamp': datetime.datetime(2015, 3, 25, 5, 31, 0, 383000,
> tzinfo=datetime.timezone(datetime.timedelta(-1, 61200))), 'time_open':
> datetime.timedelta(0, 30, 23000)}}
>
> In this case, the key is a connection id (e.g. "3424234"), and the value
> is a another dict, which contains things like 'end_timestamp',
> 'ip_address" etc.
>
> I'm writing the output of "connections" to a CSV file using DictWriter:
>
> fieldnames = ['connection_id', 'ip_address', 'open_timestamp',
> 'end_timestamp', 'time_open'] with open('output.csv', 'w') as csvfile:
> writer = DictWriter(csvfile, fieldnames)
> writer.writeheader()
> for connection, values in sorted(connections.items()):
> if 'time_open' in values:
> writer.writerow(values, {'connection_id': connection})
> else:
> pass
> # DO SOME STUFF
>
> The only problem is, I'd also like output the connection_id field as part
> of each CSV record.
>
> However, connection_id in this case is the key for the parent dict.
>
> Is there a clean way to add a extra field to DictWriter writerows, or it
> is the contents of the dict and that's it?
The latter. The obvious solution is to add the extra field to the dict:
values['connection_id'] = connection
writer.writerow(values)
Or you use a collections.ChainMap (Python 3 only I think, but
<http://code.activestate.com/recipes/305268/> should work as well here)
writer.writerow(
collections.ChainMap(values, {"connection_id": connection}))
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Using csv DictWriter - add a extra field Victor Hooi <victorhooi@gmail.com> - 2015-03-30 21:47 -0700
Re: Using csv DictWriter - add a extra field Peter Otten <__peter__@web.de> - 2015-03-31 10:22 +0200
Re: Using csv DictWriter - add a extra field Victor Hooi <victorhooi@gmail.com> - 2015-03-31 01:41 -0700
Re: Using csv DictWriter - add a extra field Peter Otten <__peter__@web.de> - 2015-03-31 11:03 +0200
csiph-web