0
我有一個讀取數據文件並返回該文件中的值作爲列表打印註釋的函數。在Python中用於文件讀取的未綁定本地錯誤
def loadcsv(filename):
"""Loads a comma-separated-value file (.csv) and returns a list of all the numerical values, ignoring comments and any malformatted data."""
"""Function should ignore bad data, but print all comments."""
datafile = open(filename)
global datafile
numbers = []
for line in datafile:
if line[0] == "#":
print line
elif line[0] != "#" or type(line[0]) != type(0) or type(line[0]) != type(0):
print "Bad Data"
else:
numbers.append(line)
datafile.close()
return numbers
不知道我是怎麼給出的錯誤我的數據文件的聲明爲全局。
太棒了!另外,還有另外一種方法可以做到這一點,無論是使用全局還是全局全局都可以在這裏使用? – alvarezcl
如果這是使用'datafile'的唯一代碼,那麼你可以刪除全局語句,它應該以同樣的方式工作。 –