在浮点数前插入逗号 python

Inserting Commas before float numbers python

我是 python 的新手。我有一个 string,如下所示:

a= "1.1 Introduction 1.2 Inverse of a Non-Singular 1.3 Elementary Transformations  1.4 Applications 1.5 Applications of Matrices"

我想在 String.

中的数字前插入逗号 我试过的代码:

a= "1.1 Introduction 1.2 Inverse of a Non-Singular 1.3 Elementary Transformations  1.4 Applications 1.5 Applications of Matrices"

b = ""
for i in a:
   print(i)
   try: 
       temp = int(i)
       b = b+ ","+ temp
   except:
       b = b + i

但这是错误的。请帮我解决一些问题。

要求输出:

"1.1 Introduction, 1.2 Inverse of a Non-Singular, 1.3 Elementary Transformations,  1.4 Applications, 1.5 Applications of Matrices"

这可能会有帮助:

import re

a= "10.1 Introduction 10.2 Inverse of a Non-Singular 1.3 Elementary Transformations  1.4 Applications 1.5 Applications of Matrices"

re.sub(r"\s+(\d+\.\d+)", r", ", a)