【发布时间】:2013-11-23 04:34:58
【问题描述】:
我将如何打印列表?
def main():
fileName = input("Please input name of file to read: ")
fileOpen = open(fileName)
lineList = fileOpen.readlines()
print("Hercules' Strategy:", lineList[len(lineList)-3].strip())
print("Initial Hydra heads:",lineList[len(lineList)- 2].strip())
print("Hydra growth period:", lineList[len(lineList)-1].strip())
main()
文本文件:
smallest
8 7 3
10
电流输出:
Hercules' Strategy: smallest
Initial Hydra heads: 8 7 3
Hydra growth period: 10
我想要得到的输出是:
Hercules' Strategy: smallest
Initial Hydra heads: [8, 7, 3]
Hydra growth period: 10
【问题讨论】:
-
这不是我认为的列表。它只是一个字符串。如果你只想要括号,为什么不
print '[' + you_string + ']' -
@gongzhitaao 是的。这就是我现在的问题。我想打印出列表,而不是字符串。
-
所以你真的想把字符串解析成一个列表?