我有一个文本文件,其中包含:
1:PAPER TOWNS,TOMORROWLAND
2:ENTOURAGE,JUPITER ASCENDING
我打算将它们读入一个输出列表:
[[1,'PAPERTOWNS','TOMORROWLAND'],[2,'ENTOURAGE','JUPITERASCENDING']]
我已经写了:
def read_file():
fileName = "testing.txt"
testFile = open(fileName)
table = []
for line in testFile:
contents = line.strip().split(':')
contents[0] = int(contents[0])
contents[1] = contents[1].replace(' ','')
table.append(contents)
print(table)
我几乎设法得到了我想要的输出,但我想不出一种方法来分离字符串:
[[1,'PAPERTOWNS,TOMORROWLAND'],[2,'ENTOURAGE,JUPITERASCENDING']]
至
[[1,'PAPERTOWNS','TOMORROWLAND'],[2,'ENTOURAGE','JUPITERASCENDING']]