By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,238 Members | 1,518 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,238 IT Pros & Developers. It's quick & easy.

Regexp problem, which pattern to use in split

P: n/a
Hi,

I have a problem which I believe is seen before:
Finding the correct pattern to use, in order to split a line correctly,
using the split function in the re module.

I'm new to regexp, and it isn't always easy to comprehend for a newbie :)

The lines I want to split are like this:
(The following is one line, even if news client splits it up:)

"abc ",,"-",,,,,"Doe, John D.",2004,"A long text, which may contain many
characters. Dots, commas, and if I'm real unlucky: maybe even
"-characters","-",32454,,

These lines are in a csv file exported from excel.
Comma is obviously the separator, but as you can see a comma might
occur between " ", and if that is the case, it should not be (a
separator).
Then I pondered upon a way of using " chars in the splitting aswell,
something like "?,"? . (optional " before and after comma), which of course
also goes wrong. " may and may not occur around the splitting comma, but
that would also match single commas inside quoted text, see example.

Any pointer will be greatly appreciated. Maybe I'm attacking this problem
the wrong way already from the start? (Not that I can see another way
myself :)

Regards
--
Hans Almåsbakk
-remove .invalid for correct email
Jul 18 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Hans Almåsbakk wrote:
These lines are in a csv file exported from excel. Any pointer will be greatly appreciated. Maybe I'm attacking this problem
the wrong way already from the start? (Not that I can see another way
myself :)

import csv


http://online.effbot.org/2003_08_01_...ook-csv-module

</F>

Jul 18 '05 #2

P: n/a
Hans Almåsbakk (14.12.2004 16:02):

Any pointer will be greatly appreciated. Maybe I'm attacking this problem
the wrong way already from the start? (Not that I can see another way
myself :)


Hans, did you try the csv module in the Python library?

Matthias
Jul 18 '05 #3

P: n/a
"Fredrik Lundh" <fr*****@pythonware.com> writes:

import csv


http://online.effbot.org/2003_08_01_...ook-csv-module


This seems be just the thing I need.

Now ofcourse, another problem arouse:
The csv module is new in Python 2.3.

hans:~# python -V
Python 2.1.3

Is there a relatively hassle-free way to get the csv module working with
2.1? The server is running Debian stable/woody, and it also seemed 2.2 can
coexist with 2.1, when I checked the distro packages, if that is any help.

Regards
--
Hans Almåsbakk
-remove .invalid for correct email
Jul 18 '05 #4

P: n/a
Hans Almåsbakk wrote:
Is there a relatively hassle-free way to get the csv module working with
2.1? The server is running Debian stable/woody, and it also seemed 2.2 can
coexist with 2.1, when I checked the distro packages, if that is any help.


2.3 and 2.4 can also coexist with 2.1 (use "make altinstall" to leave "python"
alone, so if you're using a pure-Python application, upgrading might be a good
idea.

alternatively, the following module (with a slightly different API) should work
under 2.1:

http://www.object-craft.com.au/projects/csv/

</F>

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.