[Tutor] Need some help on output

Kushal Kumaran kushal.kumaran+python at gmail.com
Fri Mar 4 02:37:19 CET 2011


On Fri, Mar 4, 2011 at 6:48 AM, Becky Mcquilling <ladymcse2000 at gmail.com> wrote:
> I am creating a dictionary by parsing a text file.
>
> The code is below:
> backup_servers = {}
> fo = open('c:/test/backup_shares.txt')
> for line in fo:
>   backup_server = line.split(',')
>   backup_servers[backup_server[0]]=backup_server[1]

Looping over a file object returns lines including the trailing
newline.  If that's not required, you have to remove the newline at
the end yourself using the strip method.

To see the behaviour, print out the value of line at the start of the loop body.

Documentation on file objects:
http://docs.python.org/library/stdtypes.html#file-objects
Documentation on strings:
http://docs.python.org/library/stdtypes.html#string-methods

> <snip>

-- 
regards,
kushal


More information about the Tutor mailing list