如何逐行读取并从python中的文件中解析?
我是 python 新手。
第一行输入是模拟次数。下一行是行数 (x),后跟一个空格,然后是列数 (y)。下一组 y 行将有 x 个字符,其中一个句点 ('.') 代表一个空格,一个大写字母“A”代表一个起始代理。
我的代码出错了
Traceback (most recent call last):
numSims = int (line)
TypeError: int() argument must be a string or a number, not 'list'
谢谢你的帮助。
输入.txt
2 --- 2 simulations
3 3 -- 3*3 map
.A. --map
AA.
A.A
2 2 --2*2 map
AA --map
.A
def main(cls, args):
numSims = 0
path = os.path.expanduser('~/Desktop/input.txt')
f = open(path)
line = f.readlines()
numSims = int (line)
print numSims
k=0
while k < numSims:
minPerCycle = 1
row = 0
col = 0
xyLine= f.readLines()
row = int(xyLine.split()[0])
col = int(xyLine.split()[1])
myMap = [[Spot() for j in range(col)] for i in range(row)]
## for-while
i = 0
while i < row:
myLine = cls.br.readLines()
## for-while
j = 0
while j < col:
if (myLine.charAt(j) == 'B'):
cls.myMap[i][j] = Spot(True)
else:
cls.myMap[i][j] = Spot(False)
j += 1
i += 1
对于 Spot.py
现货.py
class Spot(object):
isBunny = bool()
nextCycle = 0
UP = 0
RIGHT = 1
DOWN = 2
LEFT = 3
SLEEP = 4
def __init__(self, newIsBunny):
self.isBunny = newIsBunny
self.nextCycle = self.UP