如何逐行阅读并解析python中的文件?

Mik*_*ike 2 python parsing readline

如何逐行阅读并解析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'
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

INPUT.TXT

2   --- 2 simulations
3 3  -- 3*3 map
.A.  --map
AA.
A.A
2 2  --2*2 map
AA  --map
.A
Run Code Online (Sandbox Code Playgroud)
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
Run Code Online (Sandbox Code Playgroud)

对于Spot.py

Spot.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
Run Code Online (Sandbox Code Playgroud)

Mar*_*ers 7

你的错误很多,这是我迄今为止发现的错误:

  1. 这条线numSims = (int)line不符合你的想法.Python没有C强化转换,您需要调用int类型:

    numSims = int(line)
    
    Run Code Online (Sandbox Code Playgroud)

    您稍后使用大写拼写复合此错误Int:

    row = (Int)(xyLine.split(" ")[0])
    col = (Int)(xyLine.split(" ")[1])
    
    Run Code Online (Sandbox Code Playgroud)

    以类似的方式纠正这些:

    row = int(xyLine.split()[0])
    col = int(xyLine.split()[1])
    
    Run Code Online (Sandbox Code Playgroud)

    并且由于默认值.split()是在空格上拆分,因此您可以省略" "参数.更好的是,将它们组合成一行:

    row, col = map(int, xyLine.split())
    
    Run Code Online (Sandbox Code Playgroud)
  2. 你永远不会增加k,所以你的while k < numSims:循环将永远持续,所以你会得到一个EOF错误.for改为使用循环:

    for k in xrange(numSims):
    
    Run Code Online (Sandbox Code Playgroud)

    您不需要while在此函数中的任何位置使用它们,它们都可以用for variable in xrange(upperlimit):循环替换.

  3. Python字符串没有.charAt方法.[index]改为使用:

    if myLine[j] == 'A':
    
    Run Code Online (Sandbox Code Playgroud)

    但由于myLine[j] == 'A'是布尔测试,您可以Spot()像这样简化实例化:

    for i in xrange(row):
        myLine = f.readLine()
        for j in xrange(col):
            cls.myMap[i][j] = Spot(myLine[j] == 'A')
    
    Run Code Online (Sandbox Code Playgroud)
  4. 在Python中没有必要非常初始化变量.如果要在以下行中指定新值,则可以获得大部分numSims = 0col = 0行等.

  5. 您可以创建一个'myMap variable but then ignore it by referring tocls.myMap`.

  6. 这里没有处理多重地图; 文件中的最后一个地图将覆盖任何前面的地图.

改写版:

def main(cls, args):
    with open(os.path.expanduser('~/Desktop/input.txt')) as f:
        numSims = int(f.readline())
        mapgrid = []
        for k in xrange(numSims):
            row, col = map(int, f.readline().split())  
            for i in xrange(row):
                myLine = f.readLine()
                mapgrid.append([])
                for j in xrange(col):
                    mapgrid[i].append(Spot(myLine[j] == 'A'))
         # store this map somewhere?
         cls.myMaps.append(mapgrid)
Run Code Online (Sandbox Code Playgroud)