Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python Mapped Strings
#11
(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.

import csv

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 row, item in enumerate(nlist):
        if replace_this in item:
            itemx = item.split(' ')
            nitem = f"{with_this} {itemx[1]} " 
            nlist[row] = nitem
    nstr = ''.join(nlist)
    
    return(nstr)
 
def testit():

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

    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"
   
    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:
        newstring = replace_text(buf, replace_this=row[0], with_this=row[1])
    print(newstring)
 
if __name__ == '__main__':
    testit()
Reply
#12
would

newstring = buf.replace(buf, 'nrdbc', 'n3DeleteBearerCommand')
do the same for you?

are you wanting to test column 1 and based on that maybe change column 2?

oops...
that should be:
newstring = buf.replace('nrdbc', 'n3DeleteBearerCommand')
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
In code now it will overwrite all values and only save last.
I would do it something like this,making a replace_dict first then loop over and replace.
# mapp1.py
# Your .csv output append to list,can also make it dict on fly
lst = [
    ['-nrdbc', 'n3DeleteBearerCommand'],
    ['-nrrc', 'n3ContextRsp'],
]

# Make it to a dictionary
replace_dict = {key: value for key, value in lst}

input_string = "gsh modify_gtp_v2 -nrrc 3 -trac 3 -nrds 2 -trds 5 -nrdbc 2"
words = input_string.split()
output_words = []
for word in words:
    if word in replace_dict:
        output_words.append(replace_dict[word])
    else:
        output_words.append(word)

output_string = " ".join(output_words)
print(output_string)
Test.
λ python -i mapp1.py
gsh modify_gtp_v2 n3ContextRsp 3 -trac 3 -nrds 2 -trds 5 n3DeleteBearerCommand 2

>>> replace_dict
{'-nrdbc': 'n3DeleteBearerCommand', '-nrrc': 'n3ContextRsp'}

>>> # Made from
>>> lst
[['-nrdbc', 'n3DeleteBearerCommand'], ['-nrrc', 'n3ContextRsp']]
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,971 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