Path: csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!cs.uu.nl!news0.firedrake.org!news.nosignal.org!newsgate.cistron.nl!newsgate.news.xs4all.nl!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; 'author:': 0.03; 'languages,': 0.04; 'deny': 0.07; 'matches': 0.07; 'subject:help': 0.07; '#print': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'rows': 0.09; 'subject:files': 0.09; 'library': 0.15; '"python': 0.16; 'csv': 0.16; 'csv,': 0.16; 'guys,': 0.16; 'guys?': 0.16; 'line)': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'row': 0.16; 'subject:CSV': 0.16; 'two,': 0.16; 'wrote:': 0.17; 'certainly': 0.17; 'module': 0.19; 'help.': 0.22; "i'd": 0.22; "i've": 0.23; 'header': 0.24; 'header:In-Reply-To:1': 0.25; 'header:User-Agent:1': 0.26; '(which': 0.26; '----': 0.27; 'header:X-Complaints-To:1': 0.28; 'lines': 0.28; 'bad.': 0.29; 'date:': 0.29; 'this.': 0.29; 'from:addr:yahoo.co.uk': 0.32; 'could': 0.32; 'info': 0.32; 'to:addr:python-list': 0.33; '(with': 0.33; 'there': 0.35; 'received:org': 0.36; 'really': 0.36; 'alone': 0.36; 'anything': 0.36; 'bad': 0.37; 'one,': 0.37; 'subject:: ': 0.38; 'mark': 0.38; 'fact': 0.38; 'some': 0.38; 'to:addr:python.org': 0.39; 'where': 0.40; 'header:Received:5': 0.40; 'end': 0.40; 'your': 0.60; "you've": 0.61; 'here': 0.65; 'total': 0.65; 'middle': 0.66; '100': 0.78; 'subject:this': 0.84; '(they': 0.84; 'fact.': 0.84; 'received:89': 0.86 X-Injected-Via-Gmane: http://gmane.org/ To: python-list@python.org From: Mark Lawrence Subject: Re: Some help in refining this regex for CSV files Date: Thu, 06 Dec 2012 07:57:58 +0000 References: <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-Gmane-NNTP-Posting-Host: host-89-240-171-189.as13285.net User-Agent: Mozilla/5.0 (Windows NT 6.0; rv:16.0) Gecko/20121026 Thunderbird/16.0.2 In-Reply-To: <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com> X-Antivirus: avast! (VPS 121205-1, 05/12/2012), Outbound message X-Antivirus-Status: Clean 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: 36 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1354780692 news.xs4all.nl 6864 [2001:888:2000:d::a6]:46939 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:34367 On 06/12/2012 07:21, Oltmans wrote: > Hi guys, > > I've to deal with CSVs that look like following > > CSV (with one header and 3 legit rows where each legit row has 3 columns) > ---- > Some info > Date: 12/6/2012 > Author: Some guy > Total records: 100 > > header1, header2, header3 > one, two, three > one, "Python is great, so are other languages, isn't ?", three > one, two, 'some languages, are realyl beautiful\r\n, I really cannot deny \n this \t\t\t fact. \t\t\t\tthis fact alone is amazing' > ---- > > So inside this CSV, there will always be bad lines like the top 4 (they could end up in the beginning, in the middle and even in the last). So above sample, csv has 3 legit lines and a header. I want to read those three lines and here is a regex that I came up with (which clearly isn't working) > > #print line > pattern = r"([^\t]+\t|,+)" > matches = re.match(pattern, line) > > Do you've any better ideas guys? I will really appreciate all help. > I'd simply use the csv module from the standard library to read your files, discarding anything that you regard as bad. I'd certainly not use a regex for this. -- Cheers. Mark Lawrence.