I have a comma delimited text file that has multiple instances of
multiple commas. Each file will contain approximatley 300 lines. For
example:
one, two, three,,,,four,five,,,,six
one, two, three,four,,,,,,,,,,eighteen, and so on.
There is one time when multiple commas are allowed. Just prior to the
letters ADMNSRC there should be one instance of 4 commas. (
,eight,,,,ADMNSRC,thirteen, ). The text ADMNSRC is NOT in the same
place on each line.
What would be the best approach to replace all instances of multiple
commas with just one comma, except for the 4 commas prior to ADMNSRC?
Any help would be greatly appreciated.
TIA,
Kevin 4 2470
striker wrote: I have a comma delimited text file that has multiple instances of multiple commas. Each file will contain approximatley 300 lines. For example:
one, two, three,,,,four,five,,,,six one, two, three,four,,,,,,,,,,eighteen, and so on.
There is one time when multiple commas are allowed. Just prior to the letters ADMNSRC there should be one instance of 4 commas. ( ,eight,,,,ADMNSRC,thirteen, ). The text ADMNSRC is NOT in the same place on each line.
What would be the best approach to replace all instances of multiple commas with just one comma, except for the 4 commas prior to ADMNSRC?
Seems like a typical use case for the re module...
-> now you've got *2* problems- !-)
--
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'o****@xiludom.gro'.split('@')])"
On Nov 14, striker wrote: I have a comma delimited text file that has multiple instances of multiple commas. Each file will contain approximatley 300 lines. For example:
one, two, three,,,,four,five,,,,six one, two, three,four,,,,,,,,,,eighteen, and so on.
There is one time when multiple commas are allowed. Just prior to the letters ADMNSRC there should be one instance of 4 commas. ( ,eight,,,,ADMNSRC,thirteen, ). The text ADMNSRC is NOT in the same place on each line.
What would be the best approach to replace all instances of multiple commas with just one comma, except for the 4 commas prior to ADMNSRC?
One possible approach:
#! /usr/bin/env python
import re
# This list simulates the actual opened file.
infile = [
'one, two, three,four,,,,,,ADMNSRC,,,,eighteen,',
'one, two, three,four,five,six'
]
# Placeholder for resultant list.
result = []
for item in infile:
# Use a regex to just reduce *all* multi-commas to singles.
item = re.sub(r',{2,}', r',', item)
# Add back the desired commas for special case.
item = item.replace('ADMNSRC', ',,,ADMNSRC')
# Remove spaces??
item = item.replace(' ', '')
# Add to resultant list.
result.append(item)
--
_ _ ___
|V|icah |- lliott <>< md*@micah.elliott.name
" " """
On Tue, 15 Nov 2005 08:26:22 GMT, Dennis Lee Bieber <wl*****@ix.netcom.com> wrote: On 14 Nov 2005 09:43:57 -0800, "striker" <st*****@trip.net> declaimed the following in comp.lang.python:
What would be the best approach to replace all instances of multiple commas with just one comma, except for the 4 commas prior to ADMNSRC?
Simplify the problem... Start by rephrasing... Since ADMNSRC is to always have four commas leading up to it (at least, as I understand your statement of intent), you could consider three of those to be part of the string. So...
Phase one: split on commas, tossing out any null fields Phase two: replace "ADMNSRC" with ",,,ADMNSRC" Phase three: rejoin the parts that remain.
Doing this efficiently may be another matter but...
data = [ "one, two, three,,,,four,,,,ADMNSRC, five,,,,six", "one, two, three,four,,,,,,,,ADMNSRC,,,,,,eighteen, and so on" ]
result = [] for ln in data: wds = [x.strip() for x in ln.split(",") if x] for i in range(len(wds)): if wds[i] == "ADMNSRC": wds[i] = ",,,ADMNSRC" result.append(",".join(wds))
print result
Or if data is from a single file read, maybe (untested beyond what you see ;-) data = """\
... one, two, three,,,,four,,,,ADMNSRC, five,,,,six
... one, two, three,four,,,,,,,,ADMNSRC,,,,,,eighteen, and so on
... """ import re rxc = re.compile(',+') result = ',,,ADMNSRC'.join(','.join(rxc.split(s)) for s in data.split(',,,ADMNSRC')) print result
one, two, three,four,,,,ADMNSRC, five,six
one, two, three,four,,,,ADMNSRC,eighteen, and so on
Regards,
Bengt Richter
On Tue, 15 Nov 2005 08:26:22 GMT, Dennis Lee Bieber <wl*****@ix.netcom.com> wrote: On 14 Nov 2005 09:43:57 -0800, "striker" <st*****@trip.net> declaimed the following in comp.lang.python:
What would be the best approach to replace all instances of multiple commas with just one comma, except for the 4 commas prior to ADMNSRC?
Simplify the problem... Start by rephrasing... Since ADMNSRC is to always have four commas leading up to it (at least, as I understand your statement of intent), you could consider three of those to be part of the string. So...
Phase one: split on commas, tossing out any null fields Phase two: replace "ADMNSRC" with ",,,ADMNSRC" Phase three: rejoin the parts that remain.
Doing this efficiently may be another matter but...
data = [ "one, two, three,,,,four,,,,ADMNSRC, five,,,,six", "one, two, three,four,,,,,,,,ADMNSRC,,,,,,eighteen, and so on" ]
result = [] for ln in data: wds = [x.strip() for x in ln.split(",") if x] for i in range(len(wds)): if wds[i] == "ADMNSRC": wds[i] = ",,,ADMNSRC" result.append(",".join(wds))
print result
Or if data is from a single file read, maybe (untested beyond what you see ;-) data = """\
... one, two, three,,,,four,,,,ADMNSRC, five,,,,six
... one, two, three,four,,,,,,,,ADMNSRC,,,,,,eighteen, and so on
... """ import re rxc = re.compile(',+') result = ',,,ADMNSRC'.join(','.join(rxc.split(s)) for s in data.split(',,,ADMNSRC')) print result
one, two, three,four,,,,ADMNSRC, five,six
one, two, three,four,,,,ADMNSRC,eighteen, and so on
Regards,
Bengt Richter This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: SilverShadow |
last post by:
Hello,
I'm having trouble with something that may be easily remedied. I use
Cantera running on Python. I need to make multiple "Reactor()" objects
and have them assigned a different (user...
|
by: Ohaya |
last post by:
Hi,
I'm trying to understand a situation where ASP seems to be "blocking" of
"queuing" requests. This is on a Win2K Advanced Server, with IIS5.
I've seen some posts (e.g.,...
|
by: dornick |
last post by:
So I want to do the above, and I really, REALLY don't want to rewrite
the entire file. I've been working on it for a while now, and can't
for the life of me get it functioning. Basically, I want...
|
by: Bill |
last post by:
Hi
I am trying to get my listbox items to print if they stream past the one
page mark. my code is working for one page of information (if the
e.hasmorepages) is not there.
But I am having...
|
by: Michel |
last post by:
Hi, I wrote an app in .Net and I whant only 1 instance of this app open for
the user; the user open my app, do some works and try to open another
instance of my app, I whant to show a message to...
|
by: Olie |
last post by:
This post is realy to get some opinions on the best way of getting fast
comunication between multiple applications. I have scowered the web for
imformation on this subject and have just found...
|
by: =?Utf-8?B?QnJpYW4gQ29vaw==?= |
last post by:
This is an example of the data;
2007/07/27 11:00:03 ARES_INDICATION 010.050.016.002 404.2.01 (6511)
RX 74 bytes
2007/07/27 11:00:03 65 11 26 02 BC 6C AA 20 76 93 51 53 50 76 13
48...
|
by: DarthBob88 |
last post by:
I have to go through a file and replace any occurrences of a given
string with the desired string, like replacing "bug" with "feature".
This is made more complicated by the fact that I have to do...
|
by: E11esar |
last post by:
Hi there.
I am looping through a csv file and for each line, I need to replace a comma in a string that occurs between quotes.
For example:
"The", "cat", "sat", "on, the", "mat"
Here...
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
| |