aearslan
aearslan

Reputation: 168

change specific part of a string in python

I have a table having a column as below. "Top: xx,xx" part is the price of a product and I want to change the comma to dot. Like Top:26,70should be Top:26.70 Since there might be multiple occasions within the same row and there is no pattern for the rows I could not split the string.

Product
Etli Barbekü Brioche® Menü(Mktr:1,Id:20046,Top:26,70)\r\n
Whopper® Menü(Mktr:1,Id:10001,Top:26,25)\r\n,Whopper® Menü(Mktr:1,Id:10001,Top:22,5)\r\n
Köfteburger® Menü(Mktr:1,Id:10030,Top:16,95)\r\n,Köfteburger® Menü(Mktr:1,Id:10030,Top:16,95)\r\n,İndirimli Vodafone Menüsü (WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19,9)\r\n,İndirimli Vodafone Menüsü (WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19,9)\r\n,Big Royale Kampanyası(Mktr:1,Id:98449,Top:32,9)\r\n,Köfteburger® Menü(Mktr:1,Id:10030,Top:18,95)\r\n,Double Köfteburger® Menü(Mktr:1,Id:20042,Top:21,95)\r\n,Double Köfteburger® Menü(Mktr:1,Id:20042,Top:21,95)\r\n,Kral İkili(Mktr:1,Id:98176,Top:28,9
(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:99595,Top:4,5)\r\n
(Mktr:1,Id:99009,Top:28,95)\r\n,(Mktr:1,Id:99065,Top:10,75)\r\n
YENI BIG KING MENU(Mktr:1,Id:20026,Top:20,70)\r\n
(Mktr:1,Id:99928,Top:32,45)\r\n
(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n
(Mktr:1,Id:115000,Top:0,01)\r\n,Whopper Cheese Menü(Mktr:1,Id:20001,Top:22,5)\r\n,(Mktr:1,Id:98584,Top:18,25)\r\n,(Mktr:1,Id:98535,Top:4,5)\r\n,Ranch Sos(Mktr:1,Id:90008,Top:0,75)\r\n

Upvotes: 3

Views: 85

Answers (5)

Vipul Jurel
Vipul Jurel

Reputation: 41

str1 = """Product
Etli Barbekü Brioche® Menü(Mktr:1,Id:20046,Top:26,70)\r\n
Whopper® Menü(Mktr:1,Id:10001,Top:26,25)\r\n,Whopper® 
Menü(Mktr:1,Id:10001,Top:22,5)\r\n
Köfteburger® Menü(Mktr:1,Id:10030,Top:16,95)\r\n,Köfteburger® 
Menü(Mktr:1,Id:10030,Top:16,95)\r\n,İndirimli Vodafone Menüsü 
(WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19,9)\r\n,İndirimli Vodafone 
Menüsü (WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19,9)\r\n,Big Royale 
Kampanyası(Mktr:1,Id:98449,Top:32,9)\r\n,Köfteburger® 
Menü(Mktr:1,Id:10030,Top:18,95)\r\n,Double Köfteburger® 
Menü(Mktr:1,Id:20042,Top:21,95)\r\n,Double Köfteburger® 
Menü(Mktr:1,Id:20042,Top:21,95)\r\n,Kral İkili(Mktr:1,Id:98176,Top:28,9
(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:98584,Top:17,75)\r\n, 
(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:99595,Top:4,5)\r\n
(Mktr:1,Id:99009,Top:28,95)\r\n,(Mktr:1,Id:99065,Top:10,75)\r\n
YENI BIG KING MENU(Mktr:1,Id:20026,Top:20,70)\r\n
(Mktr:1,Id:99928,Top:32,45)\r\n
(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n, 
(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n, 
(Mktr:1,Id:98584,Top:14,75)\r\n
(Mktr:1,Id:115000,Top:0,01)\r\n,Whopper Cheese 
Menü(Mktr:1,Id:20001,Top:22,5)\r\n,(Mktr:1,Id:98584,Top:18,25)\r\n, 
(Mktr:1,Id:98535,Top:4,5)\r\n,Ranch Sos(Mktr:1,Id:90008,Top:0,75)\r\n"""


first_occurence = str1.index("Top")

list_top_price = []

for i in range(0, str1.count("Top")): # count the number of occurrence of "Top"
    first_occurence = str1.index("Top", first_occurence + i)
    last_occurence = str1.index(")", first_occurence + 5)
    # append all occurrence of substring start with "Top"
    list_top_price.append(str1[first_occurence: last_occurence])

str_formated = ''
tempstr = str1 # copy of str1

for t in list_top_price:
   comma_replaced = str(t).replace(",", ".") # comma replaced string
   str_formated = tempstr.replace(str(t), comma_replaced)
   tempstr = str_formated

print(str2)

OutPut :: 
Product
Etli Barbekü Brioche® Menü(Mktr:1,Id:20046,Top:26.70)

Whopper® Menü(Mktr:1,Id:10001,Top:26.25)
,Whopper® Menü(Mktr:1,Id:10001,Top:22.5)

Köfteburger® Menü(Mktr:1,Id:10030,Top:16.95)
,Köfteburger® Menü(Mktr:1,Id:10030,Top:16.95)

Upvotes: 0

Duyen Nguyen
Duyen Nguyen

Reputation: 1

This is one solution:

matches = re.findall(r"Top:\d{1,2},\d{1,2}", your_str)
for i in matches:
    new_i = i.replace(",", ".")
    your_str = your_str.replace(i, new_i)

Upvotes: 0

AnsFourtyTwo
AnsFourtyTwo

Reputation: 2528

Found a possible solution here:

import re

# Function to run on matches
def reg_replace(match_obj):
     s = match_obj.group(0)
     s = s.replace(",", ".")
     return s

pat = r"Top:\d+,\d+"
re_pat = re.compile(pat)

test = '(Mktr:1,Id:115000,Top:0,01)\r\n,Whopper Cheese Menü(Mktr:1,Id:20001,Top:22,5)\r\n,(Mktr:1,Id:98584,Top:18,25)\r\n,(Mktr:1,Id:98535,Top:4,5)\r\n,Ranch Sos(Mktr:1,Id:90008,Top:0,75)\r\n'

res = re.sub(re_pat, reg_replace, test)
res

# '(Mktr:1,Id:115000,Top:0.01)\r\n,Whopper Cheese Menü(Mktr:1,Id:20001,Top:22.5)\r\n,(Mktr:1,Id:98584,Top:18.25)\r\n,(Mktr:1,Id:98535,Top:4.5)\r\n,Ranch Sos(Mktr:1,Id:90008,Top:0.75)\r\n'

It defines a function on what to do with the matches you specify with a regular expression.

Upvotes: 0

LinPy
LinPy

Reputation: 18618

you may use this :

import re
print(re.sub(r"(Top:\d{1,}),(\d{1,})", r"\1.\2" , string))

that will only replace the , with . resulting

(Mktr:1,Id:115000,Top:0.01)
,Whopper Cheese Menü(Mktr:1,Id:20001,Top:22.5)
,(Mktr:1,Id:98584,Top:18.25)
,(Mktr:1,Id:98535,Top:4.5)
,Ranch Sos(Mktr:1,Id:90008,Top:0.75)

Upvotes: 1

kellymandem
kellymandem

Reputation: 1769

I hope this can help

import re

s = '''Product
Etli Barbekü Brioche® Menü(Mktr:1,Id:20046,Top:26,70)\r\n
Whopper® Menü(Mktr:1,Id:10001,Top:26,25)\r\n,Whopper® Menü(Mktr:1,Id:10001,Top:22,5)\r\n
Köfteburger® Menü(Mktr:1,Id:10030,Top:16,95)\r\n,Köfteburger® Menü(Mktr:1,Id:10030,Top:16,95)\r\n,İndirimli Vodafone Menüsü (WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19,9)\r\n,İndirimli Vodafone Menüsü (WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19,9)\r\n,Big Royale Kampanyası(Mktr:1,Id:98449,Top:32,9)\r\n,Köfteburger® Menü(Mktr:1,Id:10030,Top:18,95)\r\n,Double Köfteburger® Menü(Mktr:1,Id:20042,Top:21,95)\r\n,Double Köfteburger® Menü(Mktr:1,Id:20042,Top:21,95)\r\n,Kral İkili(Mktr:1,Id:98176,Top:28,9
(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:98584,Top:17,75)\r\n,(Mktr:1,Id:99595,Top:4,5)\r\n
(Mktr:1,Id:99009,Top:28,95)\r\n,(Mktr:1,Id:99065,Top:10,75)\r\n
YENI BIG KING MENU(Mktr:1,Id:20026,Top:20,70)\r\n
(Mktr:1,Id:99928,Top:32,45)\r\n
(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n,(Mktr:1,Id:98584,Top:14,75)\r\n
(Mktr:1,Id:115000,Top:0,01)\r\n,Whopper Cheese Menü(Mktr:1,Id:20001,Top:22,5)\r\n,(Mktr:1,Id:98584,Top:18,25)\r\n,(Mktr:1,Id:98535,Top:4,5)\r\n,Ranch Sos(Mktr:1,Id:90008,Top:0,75)\r\n'''

regex = re.compile(r'Top:(\d+)(,)(\d+)')

out = []
for st in s.splitlines():
    if regex.search(st):
        out.append(regex.sub(r'Top:\1.\3', st))
    else:
        out.append(st)

print('\r\n'.join(out))

Of which the output should look something like this

Product
Etli Barbekü Brioche® Menü(Mktr:1,Id:20046,Top:26.70)

Whopper® Menü(Mktr:1,Id:10001,Top:26.25)
,Whopper® Menü(Mktr:1,Id:10001,Top:22.5)

Köfteburger® Menü(Mktr:1,Id:10030,Top:16.95)
,Köfteburger® Menü(Mktr:1,Id:10030,Top:16.95)
,İndirimli Vodafone Menüsü (WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19.9)
,İndirimli Vodafone Menüsü (WhopperJr.Menü&Tavukburger)(Mktr:1,Id:98196,Top:19.9)
,Big Royale Kampanyası(Mktr:1,Id:98449,Top:32.9)
,Köfteburger® Menü(Mktr:1,Id:10030,Top:18.95)
,Double Köfteburger® Menü(Mktr:1,Id:20042,Top:21.95)
,Double Köfteburger® Menü(Mktr:1,Id:20042,Top:21.95)
,Kral İkili(Mktr:1,Id:98176,Top:28.9
(Mktr:1,Id:98584,Top:17.75)
,(Mktr:1,Id:98584,Top:17.75)
,(Mktr:1,Id:98584,Top:17.75)
,(Mktr:1,Id:99595,Top:4.5)

(Mktr:1,Id:99009,Top:28.95)
,(Mktr:1,Id:99065,Top:10.75)

YENI BIG KING MENU(Mktr:1,Id:20026,Top:20.70)

(Mktr:1,Id:99928,Top:32.45)

(Mktr:1,Id:98584,Top:14.75)
,(Mktr:1,Id:98584,Top:14.75)
,(Mktr:1,Id:98584,Top:14.75)
,(Mktr:1,Id:98584,Top:14.75)
,(Mktr:1,Id:98584,Top:14.75)

(Mktr:1,Id:115000,Top:0.01)
,Whopper Cheese Menü(Mktr:1,Id:20001,Top:22.5)
,(Mktr:1,Id:98584,Top:18.25)
,(Mktr:1,Id:98535,Top:4.5)
,Ranch Sos(Mktr:1,Id:90008,Top:0.75)

Upvotes: 1

Related Questions