我是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", 1", a)