file writing question

Bruno Desthuilliers onurb at xiludom.gro
Tue Jun 27 12:23:27 EDT 2006


diffuser78 at gmail.com wrote:
> Hi, I am a newbie so not very confident in file handling.
> 
> I want to write to a file atrributes in this fashion
> #NameOfComputer:MAC_Address:IP_Address
> 
> ------computer_details.txt
> begins-----------------------------------------
> IG1:8f00142a123c:192.168.2.101
> IG2:8f00142a124d:192.168.2.102
> IG3:8f00142a124e:192.168.2.103
> IG4:8f00142a124f:192.168.2.104
> IG5:8f00142a124a:192.168.2.105
> IG6:8f00142a124b:192.168.2.106
> ------computer_details.txt
> ends-------------------------------------------
> 
> While writing this file I insert "\n" after every line so that details
> of every new computer goes into next line for readibility.
> 
> 
> After I have to read this file. While reading this file "\n" remains
> which I dont want.

Then strip it:

for line in open('computer_details.txt'):
   name, mac, ip = line.strip().split(':')
   # ...

> Is there a neat way of writing to a file and not having "\n" ?

Yes : don't add the newline after every line. But note that this may
make the file a bit more difficult to read and parse for both your
program and you !-)

Also, may I suggest that you read the doc for the CSV module ?


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list