473,569 Members | 2,716 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

(easy question) Find and replace multiple items

Hello, i'm looking to find and replace multiple characters in a text
file (test1). I have a bunch of random numbers and i want to replace
each number with a letter (such as replace a 7 with an f and 6 with a
d). I would like a suggestion on an a way to do this. Thanks

Aug 8 '06 #1
4 1911

ds4ff1z wrote:
Hello, i'm looking to find and replace multiple characters in a text
file (test1). I have a bunch of random numbers and i want to replace
each number with a letter (such as replace a 7 with an f and 6 with a
d). I would like a suggestion on an a way to do this. Thanks
how about:
>>import string
text1 = "foo bar 12 spam joe876"
table = string.maketran s("0123456789", "uydnwkdfpx ")
text1.transla te(table)
'foo bar yd spam joepfd'

Mark Peters

Aug 8 '06 #2
ds4ff1z wrote:
Hello, i'm looking to find and replace multiple characters in a text
file (test1). I have a bunch of random numbers and i want to replace
each number with a letter (such as replace a 7 with an f and 6 with a
d). I would like a suggestion on an a way to do this. Thanks
http://aspn.activestate.com/ASPN/Coo...n/Recipe/81330

Aug 8 '06 #3
Hello, i'm looking to find and replace multiple characters in a text
file (test1). I have a bunch of random numbers and i want to replace
each number with a letter (such as replace a 7 with an f and 6 with a
d). I would like a suggestion on an a way to do this. Thanks
Well, the canonical way would be to use a tool designed to do
transformations :

tr '76' 'fd' < test1.txt out.txt

However, if it's python you want:
>>mapping = {'7': 'f', '6': 'd'}
s = "ABCDEFGHIJKLMN OPQRSTUVWXYZ123 4567890"
''.join([mapping.get(c, c) for c in s])
'ABCDEFGHIJKLMN OPQRSTUVWXYZ123 45df890'

will transform all the items found in "s" according to the
defined mapping.

Or, depending on your string length and the number of items
you're replacing:
>>for k,v in mapping.items() : s = s.replace(k,v)
may be a better choice. Or maybe they're both lousy choices. :)
Time it and choose accordingly.

-tkc


Aug 8 '06 #4

Tim Chase wrote:
Hello, i'm looking to find and replace multiple characters in a text
file (test1). I have a bunch of random numbers and i want to replace
each number with a letter (such as replace a 7 with an f and 6 with a
d). I would like a suggestion on an a way to do this. Thanks

Well, the canonical way would be to use a tool designed to do
transformations :

tr '76' 'fd' < test1.txt out.txt

However, if it's python you want:
>>mapping = {'7': 'f', '6': 'd'}
>>s = "ABCDEFGHIJKLMN OPQRSTUVWXYZ123 4567890"
>>''.join([mapping.get(c, c) for c in s])
'ABCDEFGHIJKLMN OPQRSTUVWXYZ123 45df890'

will transform all the items found in "s" according to the
defined mapping.

Or, depending on your string length and the number of items
you're replacing:
>>for k,v in mapping.items() : s = s.replace(k,v)

may be a better choice. Or maybe they're both lousy choices. :)
Time it and choose accordingly.

-tkc

Thanks for the solutions!

Aug 9 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
3739
by: Dave H | last post by:
Hello, I have a query regarding definition lists. Is it good practice semantically to use the dt and dd elements to mark up questions and answers in a frequently asked questions list, or FAQ? Here is an example of just such a usage: <dl class="faq"> <di>
3
1630
by: Sasha | last post by:
Hi everybody, I would like to hear your thoughts on the following problem. We have the following classes. Class Exam int ID* int Version* string Name
2
14895
by: Daniel | last post by:
I use an Access database to basically take data exports, import them, manipulate the data, and then turn them into exportable reports. I do this using numerous macros, and queries to get the data in the format that I need it in order to create the reports that we use. So far this has proven to be successful for the reports that I am doing and...
29
3545
by: MP | last post by:
Greets, context: vb6/ado/.mdb/jet 4.0 (no access)/sql beginning learner, first database, planning stages (I think the underlying question here is whether to normalize or not to normalize this one data field - but i'm not sure) :-) Background info:
6
1432
by: Chris | last post by:
Hi all, I've recently discovered the joys of tag :) I have a ListView which is populated with strings from a List<>, and each ListView item is linked to the relevant object in List<> using Tag. The ListView is on a parent form, and I have a child form which adds items to List<>. So I pass the List<> by ref*, the relevant objects are created...
39
1822
by: Daz | last post by:
Hello all, my question is more regarding advice on a script design. I have about 3600 entries in my database, the user submits a list, which is then checked against those in the database to confirm whether or not they already own a particular item. If they do, then it's not added to the user table, whereas if it is, then it _is_ added to...
25
2227
by: Why Tea | last post by:
Thanks to those who have answered my original question. I thought I understood the answer and set out to write some code to prove my understanding. The code was written without any error checking. --- #include <stdio.h> #include <stdlib.h> #include <string.h> typedef struct {
0
2067
by: | last post by:
I have a question about spawning and displaying subordinate list controls within a list control. I'm also interested in feedback about the design of my search application. Lots of code is at the end of this message, but I will start with an overview of the problem. I've made a content management solution for my work with a decently...
25
5378
by: bonneylake | last post by:
Hey Everyone, Well i am not sure if my question needs to be here or in coldfusion. If i have my question is in the wrong section i am sorry in advance an will move it to the correct section. Basically i have a multiple select box. An it works except for this one part i want to add to it.The first box i have is called project members which...
0
7609
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...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
0
6278
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5504
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...
0
5217
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...
0
3651
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...
0
3636
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2107
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
0
936
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.