Path: csiph.com!goblin1!goblin.stu.neva.ru!uio.no!news.tele.dk!news.tele.dk!small.news.tele.dk!newsgate.cistron.nl!newsgate.news.xs4all.nl!nzpost1.xs4all.net!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.005 X-Spam-Evidence: '*H*': 0.99; '*S*': 0.00; 'filename': 0.07; 'cc:addr :python-list': 0.09; "'w')": 0.09; 'collections': 0.09; 'csv': 0.09; 'files:': 0.09; 'output': 0.13; "'r')": 0.16; 'collections.': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'measured': 0.16; 'ordereddict': 0.16; 'row': 0.16; 'subject:continue': 0.16; 'subject:pass': 0.16; 'subject:writing': 0.16; 'wrote:': 0.16; 'input': 0.18; '(not': 0.20; '2015': 0.20; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; '(the': 0.22; 'sep': 0.22; 'file.': 0.22; 'am,': 0.23; 'import': 0.24; 'header:In-Reply-To:1': 0.24; 'all.': 0.24; 'header': 0.24; 'skip:" 20': 0.26; 'fri,': 0.27; 'message-id:@mail.gmail.com': 0.27; 'collecting': 0.27; 'fine': 0.28; 'probably': 0.31; 'problem': 0.33; 'skip:d 20': 0.34; 'received:google.com': 0.35; 'should': 0.36; 'instead': 0.36; 'subject:: ': 0.37; 'say': 0.37; 'anything': 0.38; 'files': 0.38; 'goes': 0.39; 'data': 0.39; 'some': 0.40; 'your': 0.60; 'grab': 0.64; 'subject:. ': 0.67; 'algorithm,': 0.84; 'chrisa': 0.84; 'meg,': 0.84; 'ridiculously': 0.84; "that'll": 0.84; 'subject:this': 0.85; 'to:none': 0.91; 'hundred': 0.96 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:date:message-id:subject:from:cc :content-type; bh=qi/cHRzbI5my2ctrmJpag5nzVl5Z8ruMmrd7octMEds=; b=tsvfE45N9WfRvkD/yLdL6ILI6LEaLcnG39APl0KSi9lQzgTpYPYC5h5hdifS3TwpkN skiL3nGQzqZRrj71zzR7/Cvhz1lpnjYXdPK3lSGwEkF9yU2Y1pema7xqQxLvc4FZhZSg /YckkpOP61924iI7sZ+xrRo7Xe4KBMoCayq3NBFS18JSBCKw3BOrfmMQ3ZnQ+74R/5xL wyDdcBceJyoQsQxGucYoEI93P2zwvsEb55xt7e9Zwp9DaE1N7F4Oj6QVJIwxGZmxKdCE V8T5etRM1m0PqT9hiw0WGBTAl1Kxcs4UGwz6f3uDUuH0ZaKkg71g9nnCXLs5TpyGmYYv aETQ== MIME-Version: 1.0 X-Received: by 10.107.132.144 with SMTP id o16mr3073382ioi.31.1441296687427; Thu, 03 Sep 2015 09:11:27 -0700 (PDT) In-Reply-To: <652bbe97-aef5-41dc-8f4c-cfa47bcfd120@googlegroups.com> References: <19ca6361-95fe-4a5d-84d6-c72d7941745c@googlegroups.com> <53c5301c-2833-446f-a7d1-6c0ef9314928@googlegroups.com> <652bbe97-aef5-41dc-8f4c-cfa47bcfd120@googlegroups.com> Date: Fri, 4 Sep 2015 02:11:27 +1000 Subject: Re: continue vs. pass in this IO reading and writing From: Chris Angelico Cc: "python-list@python.org" Content-Type: text/plain; charset=UTF-8 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 30 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1441296690 news.xs4all.nl 23778 [2001:888:2000:d::a6]:55824 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:95953 On Fri, Sep 4, 2015 at 1:57 AM, kbtyo wrote: > I have used CSV and collections. For some reason when I apply this algorithm, all of my files are not added (the output is ridiculously small considering how much goes in - think KB output vs MB input): > > from glob import iglob > import csv > from collections import OrderedDict > > files = sorted(iglob('*.csv')) > header = OrderedDict() > data = [] > > for filename in files: > with open(filename, 'r') as fin: > csvin = csv.DictReader(fin) > header.update(OrderedDict.fromkeys(csvin.fieldnames)) > data.append(next(csvin)) > > with open('output_filename_version2.csv', 'w') as fout: > csvout = csv.DictWriter(fout, fieldnames=list(header)) > csvout.writeheader() > csvout.writerows(data) You're collecting up just one row from each file. Since you say your input is measured in MB (not GB or anything bigger), the simplest approach is probably fine: instead of "data.append(next(csvin))", just use "data.extend(csvin)", which should grab them all. That'll store all your input data in memory, which should be fine if it's only a few meg, and probably not a problem for anything under a few hundred meg. ChrisA