Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #34367

Re: Some help in refining this regex for CSV files

From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: Some help in refining this regex for CSV files
Date 2012-12-06 07:57 +0000
References <374d15bd-b20c-431b-b9bb-37ec0b1f4df3@googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.547.1354780692.29569.python-list@python.org> (permalink)

Show all headers | View raw


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.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Some help in refining this regex for CSV files Oltmans <rolf.oltmans@gmail.com> - 2012-12-05 23:21 -0800
  Re: Some help in refining this regex for CSV files Mark Lawrence <breamoreboy@yahoo.co.uk> - 2012-12-06 07:57 +0000
  Re: Some help in refining this regex for CSV files Tim Chase <python.list@tim.thechases.com> - 2012-12-06 08:27 -0600

csiph-web