0

I want to split sentences in a document but firstly I want to find decimals,version numbers etc. in document and change the points to commas. For example:

I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.

I want change this sentence to the above form

I used the default "2,00" quality setting in Premiere 2,0,1, and specified a key frame every ten frames.

re.findall('\d*\.?\d+',s)

this code finds decimal but I couldn't change points to commas

gocen
  • 103
  • 1
  • 10

3 Answers3

0

You can use re.sub with group references. For this, first wrap the decimals in your regex into groups (...) and then use \1 and \2 in your replacement string to refer to those groups.

>>> s
'I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'
>>> re.sub(r'(\d*)\.(\d+)', r'\1,\2', s)
'I used the default "2,00" quality setting in Premiere 2,0,1, and specified a key frame every ten frames.'

Also note that by making the . optional \.? the regex could also add a , into multi-digit numbers that do not have a . at all, so better remove the ?.

However, IMHO the . in "Premiere 2.0.1" shoud not be replaced with a ,. For this, you can use negative lookbehinds and lookaheads to ensure that the number is not followed by another digit or dot.

>>> re.sub(r'(?<![\d.])(\d*)\.(\d+)(?![\d.])', r"\1,\2", s)
'I used the default "2,00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'
tobias_k
  • 81,265
  • 12
  • 120
  • 179
0

you can use tha followin code sample

 s = 'I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.I then ran the 320*240 movie'
s= re.sub(r'(\d*)\.(\d+)', r'\1,\2', s)
print(s)

output

I used the default "2,00" quality setting in Premiere 2,0,1, and specified a key frame every ten frames.I then ran the 320*240 movie
Ahmed Yousif
  • 2,298
  • 1
  • 11
  • 17
  • this code doesn't work for this string ''I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.I then ran the 320*240 movie'' – gocen Sep 18 '18 at 12:47
  • @gocen are you sure use the same code s = re.sub(r'(\d*)\.(\d+)', r'\1,\2', s) because I use it and it is working fine – Ahmed Yousif Sep 18 '18 at 13:13
0

Simple solution

import re

stringa = 'I used the default "2.00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'

Ele = re.search(r'\s*([\d.]+)', stringa)

stringa.replace(Ele.group(),Ele.group().replace(".",",",1))

The result is:

'I used the default "2,00" quality setting in Premiere 2.0.1, and specified a key frame every ten frames.'
Marco Visibelli
  • 359
  • 3
  • 7
  • thank you but I want to change all points to commas except point of sentence. I want "Premiere 2,0,1," also – gocen Sep 18 '18 at 13:08