在一行的数字末尾添加文本

时间:2018-07-07 21:49:56

标签: python python-3.x list

我已经通过从文件中提取行来创建列表,并且我试图将'.flac'添加到列表每一行中数字的末尾。

一行示例:

26-496-0000 IT WAS NOW MID JULY AND THE PLAGUE WHICH HAD CHIEFLY RAGED AT THE OTHER END OF THE TOWN AND AS I SAID BEFORE IN THE PARISHES OF SAINT GILES SAINT ANDREW'S HOLBORN

,我希望将列表中的所有此类行更改为:

26-496-0000.flac IT WAS NOW MID JULY AND THE PLAGUE WHICH HAD CHIEFLY RAGED AT THE OTHER END OF THE TOWN AND AS I SAID BEFORE IN THE PARISHES OF SAINT GILES SAINT ANDREW'S HOLBORN

关于如何为列表中每行的末尾加.flac的想法?

谢谢!

1 个答案:

答案 0 :(得分:2)

您可以使用re.sub来完成

import re
s="26-496-0000 IT WAS NOW MID JULY AND THE PLAGUE WHICH HAD CHIEFLY RAGED AT THE OTHER END OF THE TOWN AND AS I SAID BEFORE IN THE PARISHES OF SAINT GILES SAINT ANDREW'S HOLBORN"
re.sub(r'^([\d-]+)', r'\1.flac', s, flags=re.MULTILINE)
# "26-496-0000.flac IT WAS NOW MID JULY AND THE PLAGUE WHICH HAD CHIEFLY RAGED AT THE OTHER END OF THE TOWN AND AS I SAID BEFORE IN THE PARISHES OF SAINT GILES SAINT ANDREW'S HOLBORN"