Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580867
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:55:04+00:00 2026-06-11T20:55:04+00:00

How to read line by line and parse from a file in python? I

  • 0

How to read line by line and parse from a file in python?

I am new to python.

The first line of input is the number of simulations.
The next line is the number of rows(x), followed by a single space, and followed by number of columns (y).
The next group of y lines will have x number of characters, with a single period (‘.’) representing a blank space and a single capitol “A” representing a starting Agent.

My code got an error

Traceback (most recent call last):
    numSims = int (line)
TypeError: int() argument must be a string or a number, not 'list'

Thanks for your help.

Input.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

For 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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:55:05+00:00Added an answer on June 11, 2026 at 8:55 pm

    Your errors are numerous, here are the ones I’ve found so far:

    1. The line numSims = (int)line does not do what you think it does. Python does not have C casts, you need to call the int type instead:

      numSims = int(line)
      

      You compound this error later on by using an uppercase spelling of Int:

      row = (Int)(xyLine.split(" ")[0])
      col = (Int)(xyLine.split(" ")[1])
      

      Correct these in a similar manner:

      row = int(xyLine.split()[0])
      col = int(xyLine.split()[1])
      

      and since the default for .split() is to split on whitespace you can leave out the " " argument. Better yet, combine them into one line:

      row, col = map(int, xyLine.split())
      
    2. You never increment k, so your while k < numSims: loop will continue forever, so you’ll get an EOF error. Use a for loop instead:

      for k in xrange(numSims):
      

      You don’t need to use while anywhere in this function, they can all be replaced with for variable in xrange(upperlimit): loops.

    3. Python strings have no .charAt method. Use [index] instead:

      if myLine[j] == 'A':
      

      but since myLine[j] == 'A' is a boolean test, you can simplify your Spot() instantiation like so:

      for i in xrange(row):
          myLine = f.readLine()
          for j in xrange(col):
              cls.myMap[i][j] = Spot(myLine[j] == 'A')
      
    4. There is no need to initialize variables quite so much in Python. You can get of most of the numSims = 0 and col = 0 lines, etc. if you are assigning a new value on a following line.

    5. You create a ‘myMapvariable but then ignore it by referring tocls.myMap` instead.

    6. There is no handling of multible maps here; the last map in the file would overwrite any preceding map.

    Rewritten version:

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a file from which i read line by line and i have
In VB.net I'm trying to read in a specific line from a file. An
I have parsed a file in tcl and i read the line like that
I want to write a function that read line by line from a socket
I'm trying to read a line from an io in a non-blocking way. Unfortunately
Which method can be used to read one line at a time from a
The following piece of code should read each line of the file and operate
I'm using ruby to read a file and I need to somehow parse some
I would like to know how to parse XML file from local in Titanium
I have the following basic code to read a text file from a StreamReader:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.