2 回答
TA贡献1845条经验 获得超8个赞
尝试这个 :
inputFile = open("input.txt", 'r')
lineList = inputFile.readlines()
print (lineList)
fileHandle = open('inputcopy.txt', 'a')
for line in lineList:
numbers = [int(item) for item in line.split(':')[1].split(',')]
numbers.sort()
fileHandle.write("%s\n" % numbers)
fileHandle.close()
TA贡献1827条经验 获得超7个赞
稍微清理一下
for line in lineList:
# Split your line into your label and number list
label, numbers = line.strip().split(':')
# Convert the numbers into integers and sort them
numbers = [int(item) for item in numbers.split(',')]
numbers.sort()
# Convert the numbers back into a comma-delimited string
numbers = ','.join(map(str, numbers))
with open('inputcopy.txt', 'a') as f:
# Write your data back out
f.write('%s:%s\n'.format(label, numbers))
添加回答
举报