473,414 Members | 1,599 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,414 software developers and data experts.

(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 1903

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.maketrans("0123456789","uydnwkdfpx")
text1.translate(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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
''.join([mapping.get(c, c) for c in s])
'ABCDEFGHIJKLMNOPQRSTUVWXYZ12345df890'

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 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890"
>>''.join([mapping.get(c, c) for c in s])
'ABCDEFGHIJKLMNOPQRSTUVWXYZ12345df890'

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
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? ...
3
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
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...
29
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...
6
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....
39
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...
25
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....
0
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...
25
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. ...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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
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
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...
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,...
0
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...

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.