Python 3.x 使用python3更改文本中的不同单词
我想从列表中更改此文本上的“……”,但我没有得到我想要的输出 以下是我试图做的:Python 3.x 使用python3更改文本中的不同单词,python-3.x,Python 3.x,我想从列表中更改此文本上的“……”,但我没有得到我想要的输出 以下是我试图做的: #!/usr/bin/env python3 import re reg = re.compile('\.{10}') text = """ it was a (1) .......... ,cold November day. I woke up to the (2) .......... smell of (3) .......
#!/usr/bin/env python3
import re
reg = re.compile('\.{10}')
text = """
it was a (1) .......... ,cold November day.
I woke up to the (2) .......... smell of (3) ..........
roasting in the (4) .......... downstairs. I (5) ..........
down the strais to see if i could help (6) ..........
the dinner. My mome said , "See if (7) .......... needs a fresh (8) ..........
" So i carried a trat of glasses full of (9) .......... into the (10) .......... room.
Whem i got tyhere, I could'nt believe (11) .......... !
There were (12) .......... (13) .......... on the (14) .......... !
"""
replace = ['TEST0', 'TEST1', 'TEST2', 'TEST3', 'TEST4', 'TEST5', 'TEST6', 'TEST7', 'TEST8', 'TEST9', 'TEST10', 'TEST11', 'TEST12', 'TEST13']
mo = reg.findall(text)
for i in range(len(mo)) :
print(text.replace(mo[i],replace[i]))
据我所知,您需要用列表中的元素替换这些
..
。我使用string.split()
函数拆分多行文本,并逐个添加字符串,并进行相应的替换
#/usr/bin/env蟒蛇3
text=”“”
那是一个寒冷的十一月天。
我醒来时闻到了……的味道。。。。。。。。。。
在楼下烤肉。我在楼下烤肉。。。。。。。。。。
沿着街道往下走,看看我能不能帮上忙。。。。。。。。。。
晚餐。我的妈妈说:“看看是否。。。。。。。。。。需要一个新的(8)。。。。。。。。。。
“所以我拿了一大摞满是(9)……的玻璃杯进了(10)……房间。
当我来到这里时,我简直不敢相信!
有(12)…(13)…(14)!
"""
replace=['TEST0','TEST1','TEST2','TEST3','TEST4','TEST5','TEST6','TEST7','TEST8','TEST9','TEST10','TEST11','TEST12','TEST13']
text=text.split(“……”)#生成不带点的文本列表
string=”“#我们现在用内容填充的空字符串
对于范围(0,len(text)-1)中的i:
字符串+=文本[i]+替换[i]#向文本添加替换
打印(字符串)
据我所知,您需要用列表中的元素替换这些..
。我使用string.split()
函数拆分多行文本,并逐个添加字符串,并进行相应的替换
#/usr/bin/env蟒蛇3
text=”“”
那是一个寒冷的十一月天。
我醒来时闻到了……的味道。。。。。。。。。。
在楼下烤肉。我在楼下烤肉。。。。。。。。。。
沿着街道往下走,看看我能不能帮上忙。。。。。。。。。。
晚餐。我的妈妈说:“看看是否。。。。。。。。。。需要一个新的(8)。。。。。。。。。。
“所以我拿了一大摞满是(9)……的玻璃杯进了(10)……房间。
当我来到这里时,我简直不敢相信!
有(12)…(13)…(14)!
"""
replace=['TEST0','TEST1','TEST2','TEST3','TEST4','TEST5','TEST6','TEST7','TEST8','TEST9','TEST10','TEST11','TEST12','TEST13']
text=text.split(“……”)#生成不带点的文本列表
string=”“#我们现在用内容填充的空字符串
对于范围(0,len(text)-1)中的i:
字符串+=文本[i]+替换[i]#向文本添加替换
打印(字符串)
这是可行的,但有点厚颜无耻。它依赖于这样一个事实,即可以相对轻松地将原始文本转换为格式字符串。基本的“旧”字符串格式允许我们像
print("The sky is %s" % "blue")
导致:
The sky is blue
The sky is blue and the grass is green
我们可以使用多个替换占位符和值(作为元组)来扩展它。例如:
print("The sky is %s and the grass is %s" % ("blue", "green"))
导致:
The sky is blue
The sky is blue and the grass is green
通过扩展它,我们可以将其视为第一步,用占位符%s
替换所有..
。然后,我们可以通过使用更新后的输入字符串和转换为元组的替换数组来使用老式的字符串格式
text = """
it was a (1) .......... ,cold November day.
I woke up to the (2) .......... smell of (3) ..........
roasting in the (4) .......... downstairs. I (5) ..........
down the strais to see if i could help (6) ..........
the dinner. My mome said , "See if (7) .......... needs a fresh (8) ..........
" So i carried a trat of glasses full of (9) .......... into the (10) .......... room.
Whem i got tyhere, I could'nt believe (11) .......... !
There were (12) .......... (13) .......... on the (14) .......... !
"""
replace = ['TEST0', 'TEST1', 'TEST2', 'TEST3', 'TEST4', 'TEST5', 'TEST6', 'TEST7', 'TEST8', 'TEST9', 'TEST10', 'TEST11', 'TEST12', 'TEST13']
print(text.replace("..........", "%s") % tuple(replace))
这将打印出:
it was a (1) TEST0 ,cold November day.
I woke up to the (2) TEST1 smell of (3) TEST2
roasting in the (4) TEST3 downstairs. I (5) TEST4
down the strais to see if i could help (6) TEST5
the dinner. My mome said , "See if (7) TEST6 needs a fresh (8) TEST7
" So i carried a trat of glasses full of (9) TEST8 into the (10) TEST9 room.
Whem i got tyhere, I could'nt believe (11) TEST10 !
There were (12) TEST11 (13) TEST12 on the (14) TEST13 !
我想从技术上讲,您也可以通过以下方式获得与“新”格式相同的结果:
print(text.replace("..........", "{}").format(*replace))
这是可行的,但有点厚颜无耻。它依赖于这样一个事实,即可以相对轻松地将原始文本转换为格式字符串。基本的“旧”字符串格式允许我们像
print("The sky is %s" % "blue")
导致:
The sky is blue
The sky is blue and the grass is green
我们可以使用多个替换占位符和值(作为元组)来扩展它。例如:
print("The sky is %s and the grass is %s" % ("blue", "green"))
导致:
The sky is blue
The sky is blue and the grass is green
通过扩展它,我们可以将其视为第一步,用占位符%s
替换所有..
。然后,我们可以通过使用更新后的输入字符串和转换为元组的替换数组来使用老式的字符串格式
text = """
it was a (1) .......... ,cold November day.
I woke up to the (2) .......... smell of (3) ..........
roasting in the (4) .......... downstairs. I (5) ..........
down the strais to see if i could help (6) ..........
the dinner. My mome said , "See if (7) .......... needs a fresh (8) ..........
" So i carried a trat of glasses full of (9) .......... into the (10) .......... room.
Whem i got tyhere, I could'nt believe (11) .......... !
There were (12) .......... (13) .......... on the (14) .......... !
"""
replace = ['TEST0', 'TEST1', 'TEST2', 'TEST3', 'TEST4', 'TEST5', 'TEST6', 'TEST7', 'TEST8', 'TEST9', 'TEST10', 'TEST11', 'TEST12', 'TEST13']
print(text.replace("..........", "%s") % tuple(replace))
这将打印出:
it was a (1) TEST0 ,cold November day.
I woke up to the (2) TEST1 smell of (3) TEST2
roasting in the (4) TEST3 downstairs. I (5) TEST4
down the strais to see if i could help (6) TEST5
the dinner. My mome said , "See if (7) TEST6 needs a fresh (8) TEST7
" So i carried a trat of glasses full of (9) TEST8 into the (10) TEST9 room.
Whem i got tyhere, I could'nt believe (11) TEST10 !
There were (12) TEST11 (13) TEST12 on the (14) TEST13 !
我想从技术上讲,您也可以通过以下方式获得与“新”格式相同的结果:
print(text.replace("..........", "{}").format(*replace))
更简单的方法是用..
和替换列表中的添加元素拆分字符串,并生成一个完整的字符串文本
。这对我不起作用,或者我缺少一些东西。另一种方法是用..
和替换列表中的添加元素拆分字符串并生成一个完整的字符串text
。这对我不起作用,或者我遗漏了一些东西。效果很好。这正是我想要的。效果很好。这正是我想要的。我不明白这里到底发生了什么。你能再解释一下吗?我还是很新,所以我们将列表转换为一个元组,以避免创建for循环,并用tuple()的每个值替换每个“…”?或者这是一个不同的想法?@seprico我添加了一些补充说明,目的是让答案更清楚。我现在更明白了,谢谢!我不明白这里到底发生了什么你能再解释一下吗?我还是很新,所以我们将列表转换为一个元组,以避免创建for循环,并用tuple()的每个值替换每个“…”?或者这是一个不同的想法?@seprico我添加了一些补充说明,目的是让答案更清楚。我现在更明白了,谢谢!