Hi!
I've been working on a GUI for a while. The problem I'm currently having is that the files I'm reading in have the same "tags" for different lines.
For example, the following would be in a file I'm reading in:
[Laser1]
port ="//dev"
type ="lms"
[Laser2]
port ="//dev/s2"
type="hokuyu"
The way I read in files now uses a for loop- and I read in each line (and save things like the port information as variables that I use later in my code.)
Is there a way you can read in lines that says line+1, or something like that?
Ex of Reading in code:
for line in input:
if line[0:7] =="[Laser1]:
global laser1
laser1=StringVar()
laser1="on"
Thanks so much! Let me know if you need any more information!

You can use readlines() to read the entire file into a list and then access it any way you want.

input_data = open(file_name, "r").readlines()
stop = len(input_data)
for ctr in range(0, stop):
   print "This record is", ctr, input_data[ctr]
   if ctr+1 < stop:
      print "Next record is", ctr+1, input_data[ctr+1], "\n"

Note that you do not have to close a file using the open+readlines method. Python's garbage collection will do that automatically.

You can use readlines() to read the entire file into a list and then access it any way you want.

input_data = open(file_name, "r").readlines()
stop = len(input_data)
for ctr in range(0, stop):
   print "This record is", ctr, input_data[ctr]
   if ctr+1 < stop:
      print "Next record is", ctr+1, input_data[ctr+1], "\n"

Note that you do not have to close a file using the open+readlines method. Python's garbage collection will do that automatically.

I'm a little confused- how are the lines getting saved (and as what)?
Thanks!

I'm a little confused- how are the lines getting saved (and as what)?
Thanks!

You can use readlines() to read the entire file into a list

Try a
print type(input_data)
print input_data
to see what it contains

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.