Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Mapped Strings
#1
Hi experts
I have mapped, the strings below, the words that start win "-" with another string, for example "nrdbc" is equivalent to "n3DeleteBearerCommand". I would like to create a python code to match "nrdbc", without "-", and to change by "n3DeleteBearerCommand", if the word doesn't have equivalent it can be deleted with its number, example "-trsl 24" would be deleted.

gsh modify_gtp_v2 -nrrc 3 -trac 3 -nrds 2 -trds 5 -nrcs 2 -nrmb 2 -trmb 2 -nrrab 2 -trrab 1 -nri 3 -tri 3 -nrfr 2 -trfr 7 -nfrc 2 -tfrc 1 -nfac 2 -tfac 1 -nrca 2 -trca 1 -nrdbc 2 -trdbc 4 -nrmbc 2 -trmbc 4 -era on -nre 5 -tre 120 -trsl 24 -nrcn 1 -trcn 4 -nrpc 2 -trpc 7 -tnsc 10 -nns 2 -tas 1 -nnr 2 -tar 1 -nnsc 2 -tasc 1 -pb off -sncr off -trer 1 -eras off -nres 3 -tres 3000 -eris 60 -trcs 5000 -eriss 60 -nress 3 -tress 3 -eram off -erim 60 -nrem 3 -trem 3 -trgl 1 -trcss 4000 -trdss 2 -trmbs 2 -trmbcs 4 -nrcss 2 -nrdss 3 -nrmbs 2 -nrmbcs 2 -mmbr off -mabr on -nrmab 2 -trmab 2 -trml 24 -nbsig on -rnccpe true -rnccsi true -dnd 0 -hfipe false -nrcit 2 -trcit 2 -nrdit 2 -trdit 2 -tdit 6 -tavac 2 -scn false -nrbrc 2 -nrbrcs 2 -trbrc 4000 -trbrcs 4000 -gpfcl 72,73,78,83,84,86,91,100,113,120 -rlm off -atrc 10 -sots off

Thank you very much
Reply
#2
please show your code attempt, working or not. Thank you
Reply
#3
(Aug-30-2023, 06:16 PM)Larz60+ Wrote: please show your code attempt, working or not. Thank you

I need to start from the scratch but I don't know how to start.
Reply
#4
(Aug-30-2023, 06:16 PM)Larz60+ Wrote: please show your code attempt, working or not. Thank you

Something like that but for multiples string

#!/bin/python3

with open("gtp_code.txt", "r+") as file:
new_content = ""

for content in file.readlines():
new_content += content.replace("nrmab", "n3ModifyAccessBearersRequest")

file.seek(0)
file.write(new_content)
file.truncate()

print("String replace done")
Reply
#5
This was written very quickly, so probably can be improved.
You can add code to eliminate what you want from the string.
I want to leave you with something to do.

def nsplit(nstring, delimiter):
    slist = [delimiter+word.strip() for word in nstring.split(delimiter) if word]
    return slist

def replace_text(strin, replace_this, with_this):
    nlist = nsplit(strin, '-')
    for n, item in enumerate(nlist):
        if replace_this in item:
            itemx = item.split(' ')
            nitem = f"{with_this} {itemx[1]} " 
            nlist[n] = nitem
    nstr = ''.join(nlist)
    return(nstr)

def testit():
    buf = "-nrrc 3 -trac 3 -nrds 2 -trds 5 -nrcs 2 -nrmb 2 -trmb 2 -nrrab 2 " \
        "-trrab 1-nri 3 -tri 3 -nrfr 2 -trfr 7 -nfrc 2 -tfrc 1 -nfac 2 " \
        "-tfac 1 -nrca 2 -trca 1 -nrdbc 2 -trdbc 4 -nrmbc 2 -trmbc 4 -era on " \
        "-nre 5 -tre 120 -trsl 24 -nrcn 1 -trcn 4 -nrpc 2 -trpc 7 -tnsc 10 " \
        "-nns 2 -tas 1 -nnr 2 -tar 1 -nnsc 2 -tasc 1 -pb off -sncr off " \
        "-trer 1 -eras off -nres 3 -tres 3000 -eris 60 -trcs 5000 -eriss 60 " \
        "-nress 3 -tress 3 -eram off -erim 60 -nrem 3 -trem 3 -trgl 1 " \
        "-trcss 4000 -trdss 2 -trmbs 2 -trmbcs 4 -nrcss 2 -nrdss 3 -nrmbs 2 " \
        "-nrmbcs 2 -mmbr off -mabr on -nrmab 2 -trmab 2 -trml 24 -nbsig on " \
        "-rnccpe true -rnccsi true -dnd 0 -hfipe false -nrcit 2 -trcit 2 " \
        "-nrdit 2 -trdit 2 -tdit 6 -tavac 2 -scn false -nrbrc 2 -nrbrcs 2 " \
        "-trbrc 4000 -trbrcs 4000 -gpfcl 72,73,78,83,84,86,91,100,113,120 " \
        "-rlm off -atrc 10 -sots off"
    
    newstring = replace_text(buf, replace_this='nrdbc', with_this='n3DeleteBearerCommand')
    print(newstring)

if __name__ == '__main__':
    testit()
Output:
-nrrc 3-trac 3-nrds 2-trds 5-nrcs 2-nrmb 2-trmb 2-nrrab 2-trrab 1-nri 3-tri 3-nrfr 2-trfr 7-nfrc 2-tfrc 1-nfac 2-tfac 1-nrca 2-trca 1n3DeleteBearerCommand 2 -trdbc 4-nrmbc 2-trmbc 4-era on-nre 5-tre 120-trsl 24-nrcn 1-trcn 4-nrpc 2-trpc 7-tnsc 10-nns 2-tas 1-nnr 2-tar 1-nnsc 2-tasc 1-pb off-sncr off-trer 1-eras off-nres 3-tres 3000-eris 60-trcs 5000-eriss 60-nress 3-tress 3-eram off-erim 60-nrem 3-trem 3-trgl 1-trcss 4000-trdss 2-trmbs 2-trmbcs 4-nrcss 2-nrdss 3-nrmbs 2-nrmbcs 2-mmbr off-mabr on-nrmab 2-trmab 2-trml 24-nbsig on-rnccpe true-rnccsi true-dnd 0-hfipe false-nrcit 2-trcit 2-nrdit 2-trdit 2-tdit 6-tavac 2-scn false-nrbrc 2-nrbrcs 2-trbrc 4000-trbrcs 4000-gpfcl 72,73,78,83,84,86,91,100,113,120-rlm off-atrc 10-sots off
Reply
#6
Hi Larz60+
Very good, thanks

I did a map between the parameters in a csv file as below, I would like to use this csv file in "newstring = replace_text(buf, replace_this='nrdbc', with_this='n3DeleteBearerCommand')"

nre,echoRequests
nrbrc,n3BearerResourceCommand
nrcn,n3ChangeNotificationRequest
nrrc,n3ContextRsp
nrcit,n3CreateIndirectDataFwdTunnel
nrcs,n3CreateSessRequest
nrdbc,n3DeleteBearerCommand
nrdit,n3DeleteIndirectDataFwdTunnel
nrds,n3DeleteSessRequest
nri,n3Identification
nrmab,n3ModifyAccessBearersRequest
nrmbc,n3ModifyBearerCommand
Reply
#7
so what have you writen?
show code
Reply
#8
(Aug-31-2023, 10:01 PM)Larz60+ Wrote: so what have you writen?
show code

No, I don't have any code, I just mapped the parameters in a csv file and I would like to use like below

newstring = replace_text(buf, replace_this='1st string in csv', with_this='2nd string in csv'), the strings are separated by comma.
Reply
#9
you have code or you intend to have code. if you have learned enough you can at least try to make code. if you show us what you have tried (even if it immediately fails) we might get some idea of what you are thinking or what you need to change so it goes on to the next step.

i do not understand your description of what you are trying to do.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
(Sep-03-2023, 05:43 PM)Skaperen Wrote: you have code or you intend to have code. if you have learned enough you can at least try to make code. if you show us what you have tried (even if it immediately fails) we might get some idea of what you are thinking or what you need to change so it goes on to the next step.

i do not understand your description of what you are trying to do.

Ok, I will explain better

I have converted may csv file in a list with the code below

csv_file_path = '/Users/ecosta/Downloads/values.csv'

with open(csv_file_path, 'r') as file:
    csv_reader = csv.reader(file)
    data_list = []
    for row in csv_reader:
        data_list.append(row)

for row in data_list:
    print(row)

The result is:
['nre', 'echoRequests']
['nrbrc', 'n3BearerResourceCommand']
['nrcn', 'n3ChangeNotificationRequest']
['nrrc', 'n3ContextRsp']
['nrcit', 'n3CreateIndirectDataFwdTunnel']
['nrcs', 'n3CreateSessRequest']
['nrdbc', 'n3DeleteBearerCommand']
['nrdit', 'n3DeleteIndirectDataFwdTunnel']
['nrds', 'n3DeleteSessRequest']
['nri', 'n3Identification']

in the @Larz60+ code we have the below:
def testit():
    buf = "-nrrc 3 -trac 3 -nrds 2 -trds 5 -nrcs 2 -nrmb 2 -trmb 2 -nrrab 2 " \
        "-trrab 1-nri 3 -tri 3 -nrfr 2 -trfr 7 -nfrc 2 -tfrc 1 -nfac 2 " \
        "-tfac 1 -nrca 2 -trca 1 -nrdbc 2 -trdbc 4 -nrmbc 2 -trmbc 4 -era on " \
        "-nre 5 -tre 120 -trsl 24 -nrcn 1 -trcn 4 -nrpc 2 -trpc 7 -tnsc 10 " \
        "-nns 2 -tas 1 -nnr 2 -tar 1 -nnsc 2 -tasc 1 -pb off -sncr off " \
        "-trer 1 -eras off -nres 3 -tres 3000 -eris 60 -trcs 5000 -eriss 60 " \
        "-nress 3 -tress 3 -eram off -erim 60 -nrem 3 -trem 3 -trgl 1 " \
        "-trcss 4000 -trdss 2 -trmbs 2 -trmbcs 4 -nrcss 2 -nrdss 3 -nrmbs 2 " \
        "-nrmbcs 2 -mmbr off -mabr on -nrmab 2 -trmab 2 -trml 24 -nbsig on " \
        "-rnccpe true -rnccsi true -dnd 0 -hfipe false -nrcit 2 -trcit 2 " \
        "-nrdit 2 -trdit 2 -tdit 6 -tavac 2 -scn false -nrbrc 2 -nrbrcs 2 " \
        "-trbrc 4000 -trbrcs 4000 -gpfcl 72,73,78,83,84,86,91,100,113,120 " \
        "-rlm off -atrc 10 -sots off"
     
    newstring = replace_text(buf, replace_this='nrdbc', with_this='n3DeleteBearerCommand')
    print(newstring)
I would like to use the csv converted in list to pass the first string in "replace_this" and the second string in "with_this"

I think I have explained better.

Thanks guys
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print a list strings is fast but printing the joined strings is slow Skaperen 9 3,970 Aug-26-2019, 07:48 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020