Python 什么是';模块';对象不可下标";在这个代码的上下文中是什么意思?
运行test.py提供Python 什么是';模块';对象不可下标";在这个代码的上下文中是什么意思?,python,Python,运行test.py提供 Traceback (most recent call last): File "test.py", line 3, in <module> Map = Parser.Map(os.path[0] + "\\start.wmap") TypeError: 'module' object is not subscriptable test.py os.path是一个模块。不清楚您认为os.path[0]将为您做什么,因为它不是一个iterable,
Traceback (most recent call last):
File "test.py", line 3, in <module>
Map = Parser.Map(os.path[0] + "\\start.wmap")
TypeError: 'module' object is not subscriptable
test.py
os.path
是一个模块。不清楚您认为os.path[0]将为您做什么,因为它不是一个iterable,并且没有第0个元素。您尝试为os.path下标,它是一个模块。下标表示在对象上使用方括号。这是合法的,例如dict对象,但不适用于模块
错误出现在
os.path[…]
中,您无法使用键/索引查找来获取其属性,例如something[property]
是一个模块,您将其用作我认为您正在查找的列表。啊!我用的是os.path而不是sys.path!谢谢哎呀!我用的是os.path而不是sys.path!感谢所有回答的人。
import configparser
def StringIsNumber(String):
try:
int(String)
except:
return False
return True
class Map:
Parser = configparser.RawConfigParser()
MapURL = ""
def __init__(self, Map):
self.Parser.read(Map)
self.MapURL = Map
def TileTypes(self):
#All numerical sections can be assumed to be tiles
return [n for n in self.Parser.sections() if StringIsNumber(n)]
import Parser
import os
Map = Parser.Map(os.path[0] + "\\start.wmap")
print(Map.TileTypes())