python3 将一组混乱的 文字,按空格区分成单个不同单词的方法

#将字符串赚转变为数字符数组
def strtolist(str):
list = []
for i in range(len(str)):
list.append(str[i])
print(list)
print(len(list))
print('*********************************')
return list


#可以用下面的 方法,来去除多余空格
def removeblankk(line):
strr=line.strip()
l=strtolist(strr)
#中间的数组
liststr=[]
#存储单词的数组
list=[]
for i in range(len(l)):
if l[i]!=' ':
liststr.append(l[i])
if (l[i]==' 'and len(liststr)>0) or i==len(l)-1:
#由于前面已经使用了line.strip 去掉了 空格,所以 不用担心结尾有空格的情况
strrr=''.join(str(j) for j in liststr)
liststr=[]
list.append(strrr)
return list


ff=open('E:/cc.txt')
list=[]
for l in ff:
print(removeblankk(l))

第一行的输出结果为:

['a', 's', 'd', 'a', 'd', ' ', 'a', 's', 'd', ' ', ' ', 'a', 's', 'd', ' ', ' ', ' ', ' ', ' ', 'a', 's', 'd', ' ', 'a']
24
*********************************
['asdad', 'asd', 'asd', 'asd', 'a']

原文地址:https://www.cnblogs.com/alanling/p/9558269.html