Jump to content

Recommended Posts

Posted

I am wanting to read information from a text file using Python.

 

 

The format of the text file is Name Score with each result been on a newline by using “\n” for example:

 

Karl 5

 

Dave 10

 

Steve 15

 

ETC

 

 

I want to add this information into two lists one with the name and one with the numerical score.

 

 

Any ideas?

Posted

https://docs.python.org/2/tutorial/inputoutput.html

 

You open files with

 

f = open('workfile', 'r')

 

You can then loop through the file, line by line with something like :

 

for line in f:

 

For each line you want the elements out based on the space as a sep so

 

array=line.split(' ')

 

You should then have the elements as array[0] and array[1] and can append them to your separated lists.

  • Thanks 1

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...