[Tutor] Simple Python Address Book (Advice welcome!)
Wayne Werner
wayne at waynewerner.com
Sun Jun 17 13:57:08 CEST 2012
On Sun, 17 Jun 2012, Alan Gauld wrote:
> On 17/06/12 02:45, mariocatch wrote:
>> after each row?
>> #it's causing multiple line separations when writing back out
>> to file (because each
>> # time we finish reading, it ends with a trailing newline).
>> f.write("{0}{1}{2}\n".format(k, recordDelimiter, v))
>
> Sorry, you have to add a newline when writing to the file and strip it off
> when reading. Its just how it is...
If you're using Python3 or from __future__ import print_function, you can do
this
print(k,v, sep=recordDelimiter, file=f)
Which might make things a little cleaner. There's also the end parameter which
defaults to end='\n'
HTH,
Wayne
More information about the Tutor
mailing list