ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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!
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.
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.