Click to See Complete Forum and Search --> : Need help with simple python proggie
A couple of simple questions that I can't find in the Python tuts.
1. How do I print the output of my program to a text file? What is the command?
2. My program prints a word with a number appended to it, except there is a space between the word and the number,
i.e. word 100.
How would I get it to print word100 instead? At the moment the line goes
print "word",var
Thanks for the help.
Strike
06-09-2001, 10:30 PM
#!/usr/bin/env python
filename='testfile'
word='word'
number='100'
f=open(filename, 'w')
string=word + number
f.write(string)
This works for me.
Thanks Hal, that works and sent me off in a direction, but when I try and incorporate it into my prog, I get errors.
Here is what I started with:
#!/usr/bin/python
var = 0
while var < 100:
print "wordone",var
print "wordtwo",var
var = var + 1
Prints on screen what I want, except for the space between the word and the number. So I tried:
#!/usr/bin/python
var = 0
while var < 100:
print "wordone" + var
print "wordtwo" + var
var = var + 1
But get a "illegal argument type".
Anyway, further to print to a text file I tried:
#!/usr/bin/python
filename = 'outputfile'
var = 0
f = open(filename, 'w')
while var < 100:
string1 = "wordone" + var
f.write(string1)
string2 = "wordtwo" + var
f.write(string2)
var = var + 1
TypeError: read-only buffer, tuple
Can you tell me where I am screwing up? Thanks, sorry, this is so simple, but I am clueless :-(
[ 10 June 2001: Message edited by: VRay ]
Strike
06-10-2001, 06:58 AM
Well, the only error I got when I ran yours (with both PYthon 1.5.2 and 2.1) was that you can't concatenate a string and a number like that, you have to convert it to a string. So, this is what I got for your code to work (and to make the output readable, I threw in a newline for each loop iteration):
#!/usr/bin/env python
filename = 'outputfile'
var = 0
f = open(filename, 'w')
while var < 100:
string1 = "wordone" + str(var)
f.write(string1)
string2 = "wordtwo" + str(var) + "\n"
f.write(string2)
var = var + 1
Strike
06-10-2001, 06:59 AM
Also, make sure you have the correct permissions on that file. Of course, Python can do that, but it would needlessly complicate this example :)
Ben Briggs
06-10-2001, 08:40 AM
You can always do somethings like this:
while var < 100:
print "wordone%d" % var
print "wordtwo%d" % var
var = var + 1
But I would do this:
for var in range(101):
print "wordone%d" % var
print "wordtwo%d" % var
The %d tells Python the you want to pring a digit.
Ben Briggs
06-10-2001, 08:45 AM
To incorporate into you goal:
#!/usr/bin/env python
file = open('filename', 'w')
for var in range(101):
file.write("wordone%d" % var)
file.write("wordtwo%d" % var)
file.close()
Strike was on the right track, but this way's shorter.
Thanks a million guys, let me go and try these out...
Thanks again, getting closer here, those examples above work great. I am having a bit of trouble with the "/n" new line scenario. I am trying to get each word on a new line, but the prog is printing the /n instead, as in Hal's code:
string2 = "wordtwo" + str(var) + "\n"
gives me:
wordtwo10/nwordone11 etc...
instead of:
wordtwo10
wordone11 etc...
Thanks again for the help.
Haha, I got it, was using "/n" instead of "\n"....
Thanks a million again.
Ben Briggs
06-10-2001, 05:49 PM
Glad to help (and I'm sure so is Strike).
BTW, the two that helped you are me and Strike :). Hal is his status; he's a moderator of this forum :).
Strike
06-10-2001, 06:41 PM
Originally posted by Ben Briggs:
<STRONG>Glad to help (and I'm sure so is Strike).
BTW, the two that helped you are me and Strike :). Hal is his status; he's a moderator of this forum :).</STRONG>
Of course I am glad to help :)
And thanks for clarifying, Ben ;)
Strike
06-10-2001, 06:43 PM
Yeah, you have to keep in mind also that that is only maybe the 4th or 5th fully functional Python script I've written. So, I'm not really familiar with all the best ways to shorten Python programs, or the most "Pythonish" ways of doing things.
Ben Briggs
06-11-2001, 12:17 AM
You're certainly learning quicker than I did :).
BTW, you're helping people without even knowing it... when I looked at your code that you wrote for mandreko, I got the idea of how to use REs for my site (I had never used them before), now I know they're not as tough as they look :).
Strike
06-11-2001, 12:34 AM
Originally posted by Ben Briggs:
<STRONG>You're certainly learning quicker than I did :).
BTW, you're helping people without even knowing it... when I looked at your code that you wrote for mandreko, I got the idea of how to use REs for my site (I had never used them before), now I know they're not as tough as they look :).</STRONG>
Awesome. Good to hear. I always wonder how many people get help from posts but never respond. I mean, yeah, we help a lot of people here as is plain to see, but I'll bet we help a lot more than anyone thinks.
And as far as learning languages go, I do have a good amount of C++ under my belt, and I've dabbled in like 15 other languages so picking up a new one comes fairly quickly. :)