473,656 Members | 2,819 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1397
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*****@python ware.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
9165
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 RegExp object to be non-greedy as using the ? quantifier doesn't seem to work. --------- SAMPLE ----------------
20
3526
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: function GetCurrentStyle( el, prop ) { if ( window.getComputedStyle ) { // Mozilla et al return window.getComputedStyle(el, '').getPropertyValue(prop) ); } // IE5+ else if ( el.currentStyle ) {
2
987
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 an array with the 3 values in it somehow... or does that only work with reg.match some way?
8
2460
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 default, and adding a '?' after a qualifier makes it non-greedy. > The "*", "+", and "?" qualifiers are all greedy... > Adding "?" after the qualifier makes it perform the match in > non-greedy or minimal fashion...
6
2072
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 argument if the user key in a pattern , eg , and my script will search some lines that contains pat.findall(lines)
1
8365
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 you can see, there could have been a problem with the split-method. The following short article handles ways around this possible problem, that we couldn't reproduce, but someone may possibly encounter it too sometimes. If nothing else, the shown...
2
13765
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 = ":("; pattern = new RegExp (keyCombo); str = string.replace (pattern, "frown"); So, first thing I fixed is ":\(" . . . But I still get the same error!
22
2885
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 been doing it by splitting by spaces or <ptags. I run into a probelm with the richtext part of my editor. When I change the font, it wraps the text in a tag. the tag has something like <font face="arial>some words</ font This splits the text at...
0
8382
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8297
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8498
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8600
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6162
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5629
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4150
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1930
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.