gdogg371
gdogg371

Reputation: 4132

Replace one string variable with another within a wider string variable using .replace()

I have some data that looks like this:

10272,201,Halifax,1,33333,1,333,3,33
,10272,206,Barnet,2,33033,5,303,2,33
,10272,989,Forest Green,3,33311,4,331,11,31
,10272,6106,Eastleigh,4,33103,3,313,12,30
,10272,203,Lincoln,5,13330,11,13,5,330
,10272,921,Gateshead,6,33103,18,30,1,313
,10272,199,Wrexham,7,30331,14,031,4,33
,10272,164,Grimsby,8,11133,7,113,7,13
,10272,991,Woking,9,31113,8,113,8,31
,10272,749,Kidderminster,10,13311,2,331,13,11
,10272,205,Macclesfield,11,33111,12,31,6,311
,10272,1392,Aldershot,12,30311,10,31,10,031
,10272,3551,Braintree Town,13,03003,6,303,21,00
,10272,204,Torquay,14,03111,9,311,16,01
,10272,919,Southport,15,00131,16,03,14,011
,10272,185,Bristol Rovers,16,10031,13,13,17,001
,10272,213,Chester,17,00301,24,00,9,031
,10272,909,Dover,18,00130,15,03,20,010
,10272,1389,Altrincham,19,00300,17,030,22,00
,10272,982,Telford,20,10001,21,001,15,10
,10272,6140,Dartford,21,01010,20,01,19,100
,10272,1395,Welling,22,10010,19,11,23,000
,10272,913,Nuneaton,23,01000,23,00,18,100
,10272,2792,Alfreton,24,00000,22,00,24,000

I have some code that uses a reverse split to take the 5th element from the right (in the top row this is '33333') and split this with commas to give '3,3,3,3,3'.

The code that does this is (where 'match3g' resolves to the string printed above):

for line in match3g.split("\n"):
                spl = line.rsplit(",",5)[-5:-4]
                if spl:
                    spl2 = "{}".format(",".join(list(spl[0])))

So here again using the example of the top row of data, 'spl' resolves to '33333' and 'spl2' resolves to '3,3,3,3,3'. What I then want to do is substitute 'spl' with 'spl2' within the wider string 'match3g'. I have attempted to do this by adding to the above 'for' loop so that it now reads as:

for line in match3g.split("\n"):
            spl = line.rsplit(",",5)[-5:-4]
            if spl:
                spl1 = "{}".format("".join(list(spl[0])))
                spl2 = "{}".format(",".join(list(spl[0])))
                spl2 = str(spl2)
                spl1 = str(spl1)
                line = line.replace(spl1, spl2)

I have printed the values of the new string variables 'spl1' and 'spl2' to screen to confirm they are in the format I expect (which they are), but when I am attempting to use the .replace() method it is not substituting 'spl1' with 'spl2' as desired.

I thought this would work with no issues as 'match3g', 'spl1' and 'spl2' are all now strings.

Can anyone tell me what I am doing wrong?

Thanks

Upvotes: 0

Views: 50

Answers (1)

audiodude
audiodude

Reputation: 2810

Inside of your for loop, line is a variable that refers to the string in your current iteration of the list produced by match3g.split("\n"). Assigning to that variable does not have much meaning.

I assume you wish to update the 'parent' string, match3g. You cannot do this by assigning to the line variable.

What you might try is creating a new string, call it new_match3g, and append to it during every iteration of your for loop:

new_match3g = ''
for line in match3g.split("\n"):
        spl = line.rsplit(",",5)[-5:-4]
        if spl:
            spl1 = "{}".format("".join(list(spl[0])))
            spl2 = "{}".format(",".join(list(spl[0])))
            spl2 = str(spl2)
            spl1 = str(spl1)
            new_match3g += line.replace(spl1, spl2) + '\n'

Then at the end of your for loop, you will have a variable, new_match3g, that contains all of the replacements you wanted.

Hope this helps.

Upvotes: 1

Related Questions