3

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
2
  • you can try regex replace for something like "Top:number,number" to "Top:number.number" Commented Nov 6, 2019 at 9:13
  • A regular expression (module "re") should help. Commented Nov 6, 2019 at 9:14

5 Answers 5

1

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)
Sign up to request clarification or add additional context in comments.

Comments

1

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)

Comments

0

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.

Comments

0

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)

Comments

0
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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.