text file python

how to get python to open a text file and read the intergers

Page 1 of 1

2 Replies - 2609 Views - Last Post: 12 December 2010 - 01:12 PM Rate Topic: -----

#1 efregoso  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 09-December 10

text file python

Posted 11 December 2010 - 09:38 PM

Hi, so I am trying to create a text file, that holds coordinates of bricks for the game brick breaker. I have a the textfile labeled as level1, and the coordinates are
-5,9,0
-4,9,0
-3,9,0
now I am having trouble with getting python to open the file, and read the coordinates. I am not sure what command I would use to get that.
also I am not sure on how to convert them into integers once they are read. Because they are currently in string form

Is This A Good Question/Topic? 0
  • +

Replies To: text file python

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6051
  • View blog
  • Posts: 23,483
  • Joined: 23-August 08

Re: text file python

Posted 12 December 2010 - 07:25 AM

Well show us what you've done so far and perhaps we can help. Be sure to post your code IN CODE TAGS, as shown below:

:code:

This post has been edited by JackOfAllTrades: 12 December 2010 - 07:26 AM

Was This Post Helpful? 0
  • +
  • -

#3 atraub  Icon User is offline

  • Pythoneer
  • member icon

Reputation: 759
  • View blog
  • Posts: 2,010
  • Joined: 23-December 08

Re: text file python

Posted 12 December 2010 - 01:12 PM

Jack, I know normally we do require a bit more effort that this, but he did put in an effort, you can see it at This post.

nyFile = open(fileName, 'r') #filename is a String ex: "C:\\Users\\Adam\\Programming\\mytext.txt"  The 'r' stands for "Read Mode"
levels = myFile.readLines()
myFile.close()



Levels is a list where each item in the list represents 1 line from your file. You can find more information on Lists here: Introduction to Python Lists

This post has been edited by atraub: 12 December 2010 - 01:13 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1