LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-21-2004, 09:29 AM   #1
jnoller
LQ Newbie
 
Registered: Aug 2003
Posts: 2

Rep: Reputation: 0
Question Python - Set vars and loop over lines in file


Problem:

I have a text file - foo.bar inside of this file are lines of text:

x-3411342
y-1324123
w-2314121

Each with a trailing \n to designate the end of the line.

I am trying to read this file into python - which is simply in and of itself, however:

Code:
file = open("foo.bar", "rb")
while 1:
    lines = file.open().split('\n'):
    if not lines:
           break
    for lines in lines:
           key = line
           get.key(key)
           results = [key, get.up, get.down]
file.close()
Appends an extra "blank" line to the list of lines, which causes the program to crash - secondly, I've looked at about 20 examples, and all of them (including this one) suffer from the problem of looping over the lines for the number of lines.

What I want to do is open the contents of the file - read the line and set a variable - key, and for each line in the file, I want to do something with that key - but only once. So I want to read in a line, set to a variable, do something, print, and then do it for the next line.

Any thoughts, pointers?
 
Old 01-21-2004, 12:21 PM   #2
Vincent_Vega
Member
 
Registered: Nov 2003
Location: South Jersey
Distribution: Slackware, Raspbian, Manjaro
Posts: 826

Rep: Reputation: 31
I know exactly what you're talking about but cannot remember how to get around this. As you can imagine, it's not too hard, but I haven't used python in a few years...I'll keep thinking though! I'm wondering if you can make it a dictionary {} and fetch the keys instead... I vaguely remember this and now it's driving me nuts!
 
Old 01-21-2004, 10:00 PM   #3
Vincent_Vega
Member
 
Registered: Nov 2003
Location: South Jersey
Distribution: Slackware, Raspbian, Manjaro
Posts: 826

Rep: Reputation: 31
are these variables constant or do you change them regularly? If you set this up as dictionaries in the file, i.e., {'x':'3411342'} etc., you'll be able to pull out the values without having a '\n' tacked onto the end. basically, when you're entering your data, each line contains a '\n' that you don't see. When it prints to screen, or whereever, it also uses that plus it moves to the next line giving you that blank line you don't want.
Let me know if this or something else works for you.
 
Old 01-21-2004, 10:11 PM   #4
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Your code could use a few more "pythonic" touches, it'll make it shorter and clearer. Observe:

Code:
f = file("foo.bar", "rb")    # don't use open(), it's deprecated, and file() is
                             # obviously a builtin, so don't name vars file

for line in f.readlines():
    key, val = line.split('-')
     # do what you want with the key and value here
Without more details on what you want to do, I can't help a whole lot more.

Last edited by Strike; 01-21-2004 at 10:12 PM.
 
Old 02-07-2004, 01:40 AM   #5
Vincent_Vega
Member
 
Registered: Nov 2003
Location: South Jersey
Distribution: Slackware, Raspbian, Manjaro
Posts: 826

Rep: Reputation: 31
pickle module

I was thinking more along these lines:

create your file with a dictionary instead of just plain text and use the pickle module:
dict={'x':'x_val', 'y':'y_val', 'w':'w_val'}
f=file(file_name, 'w')
pickle.dump(dict, file_name)
f.close()

now to use your data:
f=file(file_name, 'r')
dict=pickle.load(f)
f.close()
for key in dict.keys():
data1, data2 = key, dict[key]

This way you'll never have the '\n' associated with your values.
 
Old 02-07-2004, 07:50 AM   #6
Strike
Member
 
Registered: Jun 2001
Location: Houston, TX, USA
Distribution: Debian
Posts: 569

Rep: Reputation: 31
Serializing is fine if you never want the user to be able to edit it. If you want a human-readable format, however ...
 
Old 02-07-2004, 10:32 AM   #7
Vincent_Vega
Member
 
Registered: Nov 2003
Location: South Jersey
Distribution: Slackware, Raspbian, Manjaro
Posts: 826

Rep: Reputation: 31
I agree. I was just passing on a different approach that would work fine in some applications.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How do set mplayer to loop by default? walterbyrd Linux - Software 3 07-24-2004 05:40 PM
newbie python question: doing more than one thing in a for loop SerfurJ Programming 1 03-18-2004 10:14 AM
how to set this lines in the boot? alexii86 Linux - Hardware 2 10-07-2003 04:54 AM
Loop over lines in text file? amaze Linux - General 2 08-12-2003 07:15 AM
how to set environment vars at GNOME terminal startup ? janisj Linux - General 8 07-30-2003 06:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 03:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration