[Tutor] Hello new to python and the mailing list, need some help

Don Arnold darnold02 at sprynet.com
Sun May 30 09:23:37 EDT 2004


Or, you could use the decorated form of print to write directly to the file:

>>> outfile = open('c:/temp2/stuff','w')
>>> print >> outfile, 'John Smith'
>>> print >> outfile, '23'
>>> print >> outfile, 'Male'
>>> outfile.close()
>>>

Contents of outfile:

John Smith
23
Male


HTH,
Don

-----Original Message-----
From: tutor-bounces+darnold02=sprynet.com at python.org
[mailto:tutor-bounces+darnold02=sprynet.com at python.org] On Behalf Of Glen
Wheeler
Sent: Sunday, May 30, 2004 12:25 AM
To: K J; tutor at python.org
Subject: Re: [Tutor] Hello new to python and the mailing list, need some
help


  Hi Kevin,

  Escape codes are special characters used in strings to represent things
which we find hard to put into simple letters.
  For example, the escape code for a newline is `\n'.  So,

print '\n'

  will actually print *two* blank lines.  There is a little inconsistency
with Python's file handling in that the print command will automatically add
a newline after any output, but a file.write(..) will not.

  As a further hint, string concatenation is formed with the `+' operator.
So that

d = 'dog'
c = 'cat'
s = c+d
print s // 'catdog'

  Now, I'm sure you can figure out how to get newlines into your file :).

  Glen

----- Original Message ----- 
From: K J
To: tutor at python.org
Sent: Sunday, May 30, 2004 1:30 PM
Subject: [Tutor] Hello new to python and the mailing list, need some help


Hello, I am new to python and need a little help. I am trying to write to a
file but wich i can the only problem that I have is that Each raw_input I
want to write to a seperate line so that it will print on the screen like
this:

Kevin
27
Male

right now it just prints to the screen like this:
Kevin27Male
here is the code that I did:
NAME = raw_input("What is your name? ")
AGE = raw_input ("What is your age? ")
SEX = raw_input ("What is your sex? ")

player = open("name.plr", 'w')
player.write (NAME)
player.write (AGE)
player.write (SEX)
player.close()

player = open("name.plr", 'r')
line = player.readline()
player.close()

print line

Thanks for any help

Kevin



_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist  -  Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor




More information about the Tutor mailing list