1

基本的に私は最初"."をに変更し"_" Name.1001.extたいName_1001.ext

私はこのようなものを持っていますが、元の名前を返しています:

print re.sub(r'\D+.\d+\.$',r'\D+_\d+\.$',fileName)
4

1 に答える 1

5

Regexこの例ではやり過ぎのように思えますが、おそらくここに行くべきstr.replace()です:

In [16]: strs="Name.1001.ext"

In [17]: strs.replace(".","_",1) # now only 1 occurrence of the 
                                 # substring is going to be replaced 
Out[17]: 'Name_1001.ext'

S.replace(old, new[, count]) -> 文字列

部分文字列 old をすべて new に置き換えた文字列 S のコピーを返します。オプションの引数 count が指定されている場合、最初の count 個の出現のみが置き換えられます。

于 2013-01-20T00:35:38.290 に答える