newFile = []
with open('test.txt', 'r') as f:
lines = f.readlines()
for line in lines:
newFile.append(line.strip().split(',')[1:3])
with open('test.txt', 'w') as f:
for line in newFile:
f.write(','.join(line) + '\n')
我希望此代码在运行时用于用户输入,以便用户输入可以存储在word中,并且可以在运行时用于for循环。
应该怎么做?
答案 0 :(得分:0)
list = [None]*3
x = True
z = 0
while z < 3:
y = raw_input("Enter a word to find its length: ")
if(y == "n"):
x = False;
else:
list[z] = y;
print len(list[z]);
z +=1
答案 1 :(得分:0)
这是一个更多的Pythonic:
result = [None] * 3
for index in range(len(result)):
y = raw_input("Enter a word to find its length: ")
if y == "n":
break
else:
result[index] = y
print len(result[index])