Path: csiph.com!usenet.pasdenom.info!weretis.net!feeder4.news.weretis.net!rt.uk.eu.org!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'python.': 0.02; 'operator': 0.03; 'output': 0.04; 'sys': 0.05; 'column': 0.07; 'exit': 0.07; 'none)': 0.07; 'python': 0.09; 'files:': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'sentence': 0.09; 'subject:files': 0.09; '{},': 0.09; '2.7': 0.13; 'exercise': 0.13; 'resulting': 0.13; 'library': 0.15; '"and"': 0.16; 'above?': 0.16; 'columns': 0.16; 'concatenate': 0.16; 'csv': 0.16; 'manageable': 0.16; 'read:': 0.16; 'received:80.91.229.3': 0.16; 'received:dip.t-dialin.net': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-dialin.net': 0.16; 'red,': 0.16; 'result:': 0.16; 'row': 0.16; 'wrote:': 0.17; 'skip': 0.17; '>>>': 0.18; 'appropriate': 0.20; 'variable': 0.20; 'are:': 0.20; 'sort': 0.21; 'import': 0.21; 'not,': 0.21; 'example': 0.23; 'insert': 0.23; 'needed.': 0.23; 'tables': 0.23; 'task': 0.23; 'thus': 0.24; 'header': 0.24; 'script': 0.24; 'header:User-Agent:1': 0.26; 'looks': 0.26; '(which': 0.26; 'wrote': 0.26; 'skip:" 20': 0.26; 'question': 0.27; 'correct': 0.28; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'record': 0.28; 'thus,': 0.29; 'no,': 0.29; 'source': 0.29; 'file': 0.32; "who's": 0.32; 'could': 0.32; 'print': 0.32; 'getting': 0.33; 'crazy': 0.33; 'district': 0.33; 'to:addr:python-list': 0.33; 'knowledge': 0.33; 'skip:- 20': 0.34; 'described': 0.35; 'skip:k 20': 0.35; 'open': 0.35; 'add': 0.36; 'received:org': 0.36; 'but': 0.36; 'programmers': 0.36; 'skip:{ 10': 0.36; 'skip:3 10': 0.37; 'skip:4 10': 0.37; 'far': 0.37; 'data': 0.37; 'subject:: ': 0.38; 'files': 0.38; 'to:addr:python.org': 0.39; 'called': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'export': 0.62; 'between': 0.63; 'email addr:gmail.com': 0.63; 'more': 0.63; 'results': 0.65; 'afraid': 0.66; 'skip:c 50': 0.66; 'subject: ': 0.66; 'records': 0.68; 'eastern': 0.71; 'goal': 0.74; 'hand': 0.82; 'district,': 0.84; 'say:': 0.84; 'southern': 0.84; 'step,': 0.84; 'western': 0.89; 'ultimate': 0.93 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Peter Otten <__peter__@web.de> Subject: Re: Grouping on and exporting to csv files Date: Wed, 20 Mar 2013 08:21:40 +0100 Organization: None References: <5c26021f-3285-400f-995d-93c2f74ee5d9@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Gmane-NNTP-Posting-Host: p50849871.dip.t-dialin.net User-Agent: KNode/4.7.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 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: 122 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363764109 news.xs4all.nl 6855 [2001:888:2000:d::a6]:39023 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41573 accessnewbie@gmail.com wrote: > My current Test_Sort.csv looks like this (which constantly > changes-although never more than 4 records, thus far anyway): > > RecNo,Count,District,Fruit,StoreNo > 1,100,0,oranges,1254 > 2,30,3,apples,654 > 3,100,0,bananas,21 > 4,0,4,grapes,478 > > I wrote the following python 2.7 script (stuck with 2.7) > > --------------------- > import sys > import csv > import operator > > > source = "E:\\Data\\Test_Sort.csv" > result = "E:\\Data\\Test_Sort1.csv" > > with open(source,"rb") as source: > rdr= csv.reader( source ) > with open(result,"wb") as result: > next(rdr, None) # Skip header > wtr= csv.writer( result ) > for r in rdr: > wtr.writerow( (r[2], r[3])) # Only write columns 3 and 4 to a > csv file > exit > exit > > data = csv.reader(open("E:\\Data\\Test_Sort1.csv"),delimiter=',') > sortedlist = sorted(data, key=operator.itemgetter(0,1)) # sort by column > 1, the 2 print sortedlist > > ----------------------------- > > My sortedlist results are: > > [['0', 'bananas'], ['0', 'oranges'], ['3', 'apples'], ['4', 'grapes']] > > I want to output all fruits with the same District number out to a csv > file called by its District number. (No headers) > > Thus in above example I would ultimately have three csv files: > > 0.csv > bananas > oranges > > 3.csv > apples > > 4.csv > grapes > > Question is : Any ideas as to how export this sortedlist to the tables > described above? The standard library offers itertools.groupby() for district, group in itertools.groupby( sortedlist, key=operator.itemgetter(0)): with open(district + ".csv", "wb") as outfile: csv.writer(outfile).writerows(group) If you want try to do it by hand as an exercise -- you need a variable to keep track of the current district: last_district = None for row in sortedlist: district = row[0] if last_district != district: # open new file/writer last_district = district # write row > I already have a script that already will look at each file and count the > number of lines and insert the correct number of commas and the word "and" > where appropriate in a sentence. > > My ultimate goal is to read each file and print out a sentence based on > whether it has record or not, and if so how many records and then add the > word "and" and/or commas between records if greater than 1. I will then > concatenate all the sentences together to form a paragraph. > > Thus, the above example would say: > > The western district sold bananas and oranges. The central district sold > apples. The southern district sold grapes. Have a look at str.join(): >>> colors = ["red", "yellow", "blue"] >>> print "Who's afraid of {}, and {}?".format(", ".join(colors[:-1]), colors[-1]) Who's afraid of red, yellow, and blue? > or if the files were: > > 0.csv > apples > bananas > oranges > > 2.csv > grapes > > The resulting paragraph would read: > > The western district sold apples, bananas, and oranges. The eastern > district sold grapes. > > It may seem like a crazy way about getting my results but it was the best > I could come up with given my limited knowledge of python. No, it is what experienced programmers do all the time -- break a complex task into manageable steps, come up with a simple solution for each step, and then improve these steps as needed.