Rocksolid Light

Welcome to novaBBS (click a section below)

mail  files  register  newsreader  groups  login

Message-ID:  

"Plan to throw one away. You will anyway." -- Fred Brooks, "The Mythical Man Month"


devel / comp.lang.python / Re: How to replace characters in a string?

SubjectAuthor
* Re: How to replace characters in a string?Dave
`* Re: How to replace characters in a string?Dennis Lee Bieber
 `- Re: How to replace characters in a string?Greg Ewing

1
Re: How to replace characters in a string?

<mailman.574.1654679526.20749.python-list@python.org>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18521&group=comp.lang.python#18521

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail
From: dav...@looktowindward.com (Dave)
Newsgroups: comp.lang.python
Subject: Re: How to replace characters in a string?
Date: Wed, 8 Jun 2022 11:09:05 +0200
Lines: 55
Message-ID: <mailman.574.1654679526.20749.python-list@python.org>
References: <B1F63761-AA3A-4B9D-B135-ED80C56E015A@looktowindward.com>
<CAPTjJmqynw1UqQk2wXMOhdRocyRursBhUnAouZLc0hMVDNGA+w@mail.gmail.com>
<0CBD9609-D7E7-4DF3-82E1-524367FB844C@looktowindward.com>
Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.21\))
Content-Type: text/plain;
charset=utf-8
Content-Transfer-Encoding: quoted-printable
X-Trace: news.uni-berlin.de AmSr4UNfPF6M/f/afTd4iAumcsj5x78k/Q7/uMMWxBbw==
Return-Path: <dave@looktowindward.com>
X-Original-To: python-list@python.org
Delivered-To: python-list@mail.python.org
Authentication-Results: mail.python.org; dkim=none reason="no signature";
dkim-adsp=none (unprotected policy); dkim-atps=neutral
X-Spam-Status: OK 0.050
X-Spam-Evidence: '*H*': 0.90; '*S*': 0.00; 'python?': 0.03; '2022':
0.05; 'string': 0.07; 'angelico': 0.09; 'string,': 0.09;
'typeerror:': 0.09; 'url:mailman': 0.15; '2022,': 0.16; '>>>>':
0.16; 'chrisa': 0.16; 'integer': 0.16; 'solved': 0.16; 'step,':
0.16; 'stick': 0.16; 'subject:characters': 0.16; 'subject:string':
0.16; 'wrote:': 0.16; 'python': 0.16; 'probably': 0.17; 'to:addr
:python-list': 0.20; 'languages': 0.22; 'basically': 0.22;
'returns': 0.22; 'version': 0.23; 'subject:How': 0.23; 'url-
ip:188.166.95.178/32': 0.25; 'url-ip:188.166.95/24': 0.25;
'actual': 0.25; 'url:listinfo': 0.25; 'cannot': 0.25; 'url-
ip:188.166/16': 0.25; 'tried': 0.26; 'jun': 0.26; 'object': 0.26;
'chris': 0.28; 'it,': 0.29; 'error': 0.29; 'seem': 0.31; 'url-
ip:188/8': 0.31; "doesn't": 0.32; 'but': 0.32; 'there': 0.33;
'same': 0.34; 'printing': 0.34; 'header:In-Reply-To:1': 0.34;
'following': 0.35; 'change': 0.36; "it's": 0.37; 'way': 0.38;
'thanks': 0.38; 'wed,': 0.39; 'best': 0.61; 'method': 0.61;
'gives': 0.62; 'i\xe2\x80\x99ve': 0.62; 'true': 0.63; 'copy':
0.63; 'spam': 0.65; 'back': 0.67; 'received:217': 0.67;
'received:216': 0.67; 'interpreted': 0.69;
'received:phx3.secureserver.net': 0.69;
'received:prod.phx3.secureserver.net': 0.69; 'skip:\xe2 10': 0.71;
'(you': 0.76; 'received:94': 0.76; '8bit%:35': 0.81;
'received:secureserver.net': 0.84; 'characters': 0.84;
'received:216.69': 0.84; 'received:216.69.139': 0.84;
'received:me': 0.84; 'so:': 0.84; 'strings': 0.84; 'this!': 0.84;
'received:217.61': 0.91
X-CMAE-Analysis: v=2.4 cv=Zusol/3G c=1 sm=1 tr=0 ts=62a06737
a=EhJYbXVJKsomWlz4CTV+qA==:117 a=y7DvasQWGNOZJvXGlv4pTw==:17
a=xUEgCuPwUUcA:10 a=IkcTkHD0fZMA:10 a=JPEYwPQDsx4A:10 a=pGLkceISAAAA:8
a=ZyfxOTdOAAAA:8 a=8AHkEIZyAAAA:8 a=jxrDfB9wqLEr9sFhQqcA:9 a=QEXdDO2ut3YA:10
a=OGAothflpLNn0Wj4icuf:22
X-SECURESERVER-ACCT: dave@looktowindward.com
X-SID: yrgcnWG7Y2KH0
In-Reply-To: <CAPTjJmqynw1UqQk2wXMOhdRocyRursBhUnAouZLc0hMVDNGA+w@mail.gmail.com>
X-Mailer: Apple Mail (2.3445.104.21)
X-Mailcore-Auth: 407226104
X-Mailcore-Domain: 182562
X-123-reg-Authenticated: dave@looktowindward.com
X-Originating-IP: 217.61.226.69
X-CMAE-Envelope: MS4xfI7yc3CaivAQDwgw7mSuB5vP+ni+mocJRtkh/REr8pNTIWAGoYcN2YH1k08uRRw55vkSeV6rovmayCLO+8qC6yyPdbDVP3Jvr5NFZy2WO+0mMQXXhS3L
5DdUbh2p9qv3j1hhVEPMl8TSQOenWZFo03LPV5Gg3yyifSGUqoOVLpdszJob6X0lI6arvaspwnPayL0/vzoEKev+MOORWzpaouw=
X-BeenThere: python-list@python.org
X-Mailman-Version: 2.1.39
Precedence: list
List-Id: General discussion list for the Python programming language
<python-list.python.org>
List-Unsubscribe: <https://mail.python.org/mailman/options/python-list>,
<mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive: <https://mail.python.org/pipermail/python-list/>
List-Post: <mailto:python-list@python.org>
List-Help: <mailto:python-list-request@python.org?subject=help>
List-Subscribe: <https://mail.python.org/mailman/listinfo/python-list>,
<mailto:python-list-request@python.org?subject=subscribe>
X-Mailman-Original-Message-ID: <0CBD9609-D7E7-4DF3-82E1-524367FB844C@looktowindward.com>
X-Mailman-Original-References: <B1F63761-AA3A-4B9D-B135-ED80C56E015A@looktowindward.com>
<CAPTjJmqynw1UqQk2wXMOhdRocyRursBhUnAouZLc0hMVDNGA+w@mail.gmail.com>
 by: Dave - Wed, 8 Jun 2022 09:09 UTC

Hi,

Thanks for this!

So, is there a copy function/method that returns a MutableString like in objective-C? I’ve solved this problems before in a number of languages like Objective-C and AppleScript.

Basically there is a set of common characters that need “normalizing” and I have a method that replaces them in a string, so:

myString = [myString normalizeCharacters];

Would return a new string with all the “common” replacements applied.

Since the following gives an error :

myString = 'Hello'
myNewstring = myString.replace(myString,'e','a’)

TypeError: 'str' object cannot be interpreted as an integer

I can’t see of a way to do this in Python?

All the Best
Dave

> On 8 Jun 2022, at 10:14, Chris Angelico <rosuav@gmail.com> wrote:
>
> On Wed, 8 Jun 2022 at 18:12, Dave <dave@looktowindward.com> wrote:
>
>> I tried the but it doesn’t seem to work?
>> myCompareFile1 = ascii(myTitleName)
>> myCompareFile1.replace("\u2019", "'")
>
> Strings in Python are immutable. When you call ascii(), you get back a
> new string, but it's one that has actual backslashes and such in it.
> (You probably don't need this step, other than for debugging; check
> the string by printing out the ASCII version of it, but stick to the
> original for actual processing.) The same is true of the replace()
> method; it doesn't change the string, it returns a new string.
>
>>>> word = "spam"
>>>> print(word.replace("sp", "h"))
> ham
>>>> print(word)
> spam
>
> ChrisA
> --
> https://mail.python.org/mailman/listinfo/python-list

Re: How to replace characters in a string?

<fpn1ahheqa7ic76rn5cvln9f4v8oe74m8r@4ax.com>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18537&group=comp.lang.python#18537

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!weretis.net!feeder6.news.weretis.net!news.misty.com!border2.nntp.dca1.giganews.com!nntp.giganews.com!buffer2.nntp.dca1.giganews.com!news.giganews.com.POSTED!not-for-mail
NNTP-Posting-Date: Wed, 08 Jun 2022 12:55:10 -0500
From: wlfr...@ix.netcom.com (Dennis Lee Bieber)
Newsgroups: comp.lang.python
Subject: Re: How to replace characters in a string?
Date: Wed, 08 Jun 2022 13:55:10 -0400
Organization: IISS Elusive Unicorn
Message-ID: <fpn1ahheqa7ic76rn5cvln9f4v8oe74m8r@4ax.com>
References: <B1F63761-AA3A-4B9D-B135-ED80C56E015A@looktowindward.com> <CAPTjJmqynw1UqQk2wXMOhdRocyRursBhUnAouZLc0hMVDNGA+w@mail.gmail.com> <0CBD9609-D7E7-4DF3-82E1-524367FB844C@looktowindward.com> <mailman.574.1654679526.20749.python-list@python.org>
User-Agent: ForteAgent/8.00.32.1272
MIME-Version: 1.0
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: 8bit
Lines: 84
X-Usenet-Provider: http://www.giganews.com
X-Trace: sv3-vDwoh0klRDV6v+p08UHG9g0v9BXFzzk+s2tntnW6WCMGLaqMz/19GGsLcEzw/uYgwj4UNKwLx844B9h!CxTU21A2a37Qbq0ny2Dx9+CLmhqdaj9E0Lk5z0tCkpMLwtqwkM97aa0tqcyJ7e2DTZTuHrtR
X-Complaints-To: abuse@giganews.com
X-DMCA-Notifications: http://www.giganews.com/info/dmca.html
X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers
X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly
X-Postfilter: 1.3.40
X-Original-Bytes: 4400
 by: Dennis Lee Bieber - Wed, 8 Jun 2022 17:55 UTC

On Wed, 8 Jun 2022 11:09:05 +0200, Dave <dave@looktowindward.com> declaimed
the following:

>Hi,
>
>Thanks for this!
>
>So, is there a copy function/method that returns a MutableString like in objective-C? I’ve solved this problems before in a number of languages like Objective-C and AppleScript.

There are no mutable strings in Python. Any operation manipulating a
string RETURNS A MODIFIED NEW STRING.

>myString = 'Hello'
>myNewstring = myString.replace(myString,'e','a’)
>

Please study the library reference manual -- it should be clear what
the various string methods can perform. Hint: they are "methods", which
means whatever is before the . becomes the automatic "self" argument inside
the method)

https://docs.python.org/3/library/stdtypes.html#string-methods

"""
str.replace(old, new[, count])

Return a copy of the string with all occurrences of substring old
replaced by new. If the optional argument count is given, only the first
count occurrences are replaced.
"""

myNewstring = myString.replace("e", "a")

However... Please study
"""
static str.maketrans(x[, y[, z]])

This static method returns a translation table usable for
str.translate().

If there is only one argument, it must be a dictionary mapping Unicode
ordinals (integers) or characters (strings of length 1) to Unicode
ordinals, strings (of arbitrary lengths) or None. Character keys will then
be converted to ordinals.

If there are two arguments, they must be strings of equal length, and
in the resulting dictionary, each character in x will be mapped to the
character at the same position in y. If there is a third argument, it must
be a string, whose characters will be mapped to None in the result.
"""
"""
str.translate(table)

Return a copy of the string in which each character has been mapped
through the given translation table. The table must be an object that
implements indexing via __getitem__(), typically a mapping or sequence.
When indexed by a Unicode ordinal (an integer), the table object can do any
of the following: return a Unicode ordinal or a string, to map the
character to one or more other characters; return None, to delete the
character from the return string; or raise a LookupError exception, to map
the character to itself.

You can use str.maketrans() to create a translation map from
character-to-character mappings in different formats.

See also the codecs module for a more flexible approach to custom
character mappings.
"""

Hmmm, I'm out-of-date... I'm on v3.8 and .removeprefix() and
..removesuffix() (from v3.9) simplify my previous post... Instead of

if myString.lower().endswith(".mp3"): #lower() is a precaution for case
myString = myString[:-4]

just use
myString = myString.lower().removesuffix(".mp3")
{note, you'll have to make the compare using .lower() on the other name
since this statement returns a lowercased version}

--
Wulfraed Dennis Lee Bieber AF6VN
wlfraed@ix.netcom.com http://wlfraed.microdiversity.freeddns.org/

Re: How to replace characters in a string?

<jgd52tFdcf9U1@mid.individual.net>

  copy mid

https://www.novabbs.com/devel/article-flat.php?id=18545&group=comp.lang.python#18545

  copy link   Newsgroups: comp.lang.python
Path: i2pn2.org!i2pn.org!news.swapon.de!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail
From: greg.ew...@canterbury.ac.nz (Greg Ewing)
Newsgroups: comp.lang.python
Subject: Re: How to replace characters in a string?
Date: Thu, 9 Jun 2022 14:43:40 +1200
Lines: 11
Message-ID: <jgd52tFdcf9U1@mid.individual.net>
References: <B1F63761-AA3A-4B9D-B135-ED80C56E015A@looktowindward.com>
<CAPTjJmqynw1UqQk2wXMOhdRocyRursBhUnAouZLc0hMVDNGA+w@mail.gmail.com>
<0CBD9609-D7E7-4DF3-82E1-524367FB844C@looktowindward.com>
<mailman.574.1654679526.20749.python-list@python.org>
<fpn1ahheqa7ic76rn5cvln9f4v8oe74m8r@4ax.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=UTF-8; format=flowed
Content-Transfer-Encoding: 7bit
X-Trace: individual.net /l/b0CLHT2EqkUxM9H7SCw7gELkDxKQgUFGfQPv7zrkHSKW6t8
Cancel-Lock: sha1:og5bQgLSu1N95gIdLSSxZYQgsFs=
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.12; rv:91.0)
Gecko/20100101 Thunderbird/91.3.2
Content-Language: en-US
In-Reply-To: <fpn1ahheqa7ic76rn5cvln9f4v8oe74m8r@4ax.com>
 by: Greg Ewing - Thu, 9 Jun 2022 02:43 UTC

On 9/06/22 5:55 am, Dennis Lee Bieber wrote:

> There are no mutable strings in Python.

If you really want a mutable sequence of characters, you can
use array.array, but you won't be able to use it directly in
place of a string in most contexts.

--
Greg

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor