我有这样做我想要它(拿一个文件,洗牌的中间字母和重新加入他们),但由于某种原因,即使我要求它分割空间的空间被删除。这是为什么?删除空格的Python拆分方法....为什么?
import random
File_input= str(input("Enter file name here:"))
text_file=None
try:
text_file = open(File_input)
except FileNotFoundError:
print ("Please check file name.")
if text_file:
for line in text_file:
for word in line.split(' '):
words=list (word)
Internal = words[1:-1]
random.shuffle(Internal)
words[1:-1]=Internal
Shuffled=' '.join(words)
print (Shuffled, end='')
这是split'的'性质。分隔符被视为不需要的分隔符并丢弃。 – Joseph
那么有没有办法保持它? – Nick
查看我提供的答案,它显示了两种方法,具体取决于您要查找哪种结果列表。 – Joseph