我在将文件读入列表时遇到问题,当我这样做时,它只会从整个文件中创建一个项目,而不是将每个元素读入其自己的字段中。我正在使用\n
作为剥离的东西,但我无法让它正常工作。
temp = open('drugs')
drugs = [temp.read().strip("\n")]
temp.close
结果:
['40 Stimpak\n53 Mentats\n87 Buffout\n109 Rad-X\n125 Booze\n260 Jet Antidote\n311 Roentgen Rum\n424 Monument Chunk\n480 Bonus +1 Agility\n525 Hypo \n48 RadAway\n71 Fruit\n103 Iguana-on-a-stick\n110 Psycho\n144 Super Stimpak\n273 Healing Powder\n334 Poison\n469 Rot Gut\n481 Bonus +1 Intelligence \n49 Antidote\n81 Iguana-on-a-stick\n106 Nuka-Cola\n124 Beer\n259 Jet\n310 Gamma Gulp Beer\n378 Cookie\n473 Mutated Toe\n482 Bonus +1 Strength ']
drugs.strip('\n')
Traceback (most recent call last):
File "seek", line 18, in <module>
print drugs.strip('\n')
AttributeError: 'list' object has no attribute 'strip'