将一篇文章的每一句话的单词进行逆序,但是标点符号不变
我不明白如何将逗号进行保留,求解。 那不是保留了么 qq1151985918 发表于 2022-5-1 15:49那不是保留了么
我写的全都变成句号了 zesty0338 发表于 2022-5-1 16:33
我写的全都变成句号了
把最后 for 循环 到 txt_d 的代码改成这部分试试看:
for i in txt_b:
s = "." if len(i) <= 1 else ","
l = len(i)
for j in range(l):
if j == l - 1 and s == ",":
s = "."
j = i.split()[::-1]
j = ' '.join(j) + s
txt_c.append(j)
txt_d = "".join(txt_c)
from string import punctuation
from re import split
with open('D:/english_str.txt', 'r') as fp:
txt = fp.read()
symbol =
words = []
for i in split(f'[{punctuation}]', txt):
t = ' '.join(i.split()[::-1])
words.append(t)
print(''.join([''.join(i) for i in zip(words, symbol)]))
页:
[1]