473,396 Members | 1,815 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Regexp problem, which pattern to use in split

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
4 1383
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
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
"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Jane Doe | last post by:
Hello, I need to browse a list of hyperlinks, each followed by an author, and remove the links only for certain authors. 1. I searched the archives on Google, but didn't find how to tell the...
20
by: RobG | last post by:
I'm messing with getPropertyValue (Mozilla et al) and currentStyle (IE) and have a general function (slightly modified from one originally posted by Steve van Dongen) for getting style properties:...
2
by: Tamas Nyilanszky | last post by:
hi, i need to filter (e.g.) the number 149, 1307 and 3100 from a line like this one: <td id=l2 title=149>1307/3100</td> so i will be able to split that string with reg.split() and receive...
8
by: John Hazen | last post by:
I want to match one or two instances of a pattern in a string. According to the docs for the 're' module ( http://python.org/doc/current/lib/re-syntax.html ) the '?' qualifier is greedy by...
6
by: micklee74 | last post by:
hi i created a script to ask user for an input that can be a pattern right now, i use re to compile that pattern pat = re.compile(r"%s" %(userinput) ) #userinput is passed from command line...
1
Atli
by: Atli | last post by:
The following small HowTo is a compilation of an original problem in getting some cookie-values through different methods of string-handling. The original Problem was posted as follows: As...
2
by: gypsy3001 | last post by:
Today, I was trying to construct a regular express in JavaScript. But I kept getting the "Error: unterminated parenthetical" message. I want to do something like this: keyCombo = ":(";...
22
by: SmokeWilliams | last post by:
Hi, I am working on a Spell checker for my richtext editor. I cannot use any open source, and must develop everything myself. I need a RegExp pattern to split text into a word array. I have...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.