JavaScript
x
14
14
1
with open("test.234.txt", 'r') as f:
2
a=f.read()
3
4
with open("test234(double space replaced by singleones)" ,"w+") as f:
5
for i in range(len(a)-1):
6
if( a[i]+a[i+1] == " "):
7
a[i].replace(a[i],"")
8
a[i+1].replace(a[i+1]," ")
9
f.write(a[i]+a[i+1])
10
11
else:
12
f.write(a[i])
13
14
Advertisement
Answer
Split string by default splits string at spaces and ignores multiple consecutive spaces:
JavaScript
1
7
1
with open("test.234.txt", 'r') as f:
2
a=f.read()
3
4
with open("test234(double space replaced by singleones)" ,"w+") as f:
5
for i in range(len(a)-1):
6
f.write(' '.join(a[i].split()))
7