3 Replies - 4464 Views - Last Post: 13 December 2011 - 08:46 PM Rate Topic: -----

#1 MangoTux  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 06-April 11

How to read multiple values in from a line in a file?

Posted 13 December 2011 - 07:37 PM

Today is my first day with Python. I have a code that reads in data from a line (With a set number of values per line), and assigns it to a 2-dimensional array. However, the file.next() only (AFAIK) reads in entire lines. Is there a function that gets around this, or do I have to separate the line into its individual elements?

Thanks!
Is This A Good Question/Topic? 0
  • +

Replies To: How to read multiple values in from a line in a file?

#2 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: How to read multiple values in from a line in a file?

Posted 13 December 2011 - 08:18 PM

According to the docs, the file object is itself an iterator that does iterate line by line.

Quote

This method returns the next input line, or raises StopIteration when EOF is hit when the file is open for reading (behavior is undefined when the file is open for writing).


So, unless you change the functionality of the iterator itself, I think you are stuck with this functionality.
Was This Post Helpful? 0
  • +
  • -

#3 MangoTux  Icon User is offline

  • D.I.C Head

Reputation: 5
  • View blog
  • Posts: 58
  • Joined: 06-April 11

Re: How to read multiple values in from a line in a file?

Posted 13 December 2011 - 08:20 PM

Alright, thanks!

Is there any way you'd recommend, then, to separate each value?
Was This Post Helpful? 0
  • +
  • -

#4 Dogstopper  Icon User is offline

  • The Ninjaducky
  • member icon



Reputation: 2870
  • View blog
  • Posts: 11,025
  • Joined: 15-July 08

Re: How to read multiple values in from a line in a file?

Posted 13 December 2011 - 08:46 PM

In each line, use the .split() command. By default, it uses whitespace to separate, but it can split on any RegEx String you provide.

http://docs.python.o...#string-methods
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1