By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,362 Members | 1,365 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,362 IT Pros & Developers. It's quick & easy.

Re: Reading a file

P: n/a
aditya shukla wrote:
I have a text file whose contents are like this:-

jd|fj|dnv|jd|0.33|c:\\windows\\win32
shcbsbs|nscsjsj|0.93|hsbcjsnc

I am trying to read the file like this:-
>x = open("c:\\a.txt","r")
Better to use "rb" (for binary read) rather than "r" mode.
>x.read()
Now, tell us what you *really* did. Not that -- that would read the
contents of the file, put throw the returned result away.
You must have done something like

c = x.read()
>
the result that i get is ike this:-
'jd|fj|dnv|jd|0.33|c:\\\\windows\\\\win32\nshcbsbs |nscsjsj|0.93|hsbcjsnc\n'

My doubt is how can i read the file as it is?
You *have* read the file as it is. Examine each character, and you'll
see that the string is exactly as you expect.

However, your output to the screen is escaping some character with
backslashes. But that's just because of the method you used to print to
the screen.

What method *did* you use to print?

If you just typed the variable into which you had read the contents,
then you get the equivalent of

print repr(c)
which explains the escapes.

Try
print c
and that won't happen.
Gary Herron
>
ie my output should be

d|fj|dnv|jd|0.33|c:\\windows\\win32
shcbsbs|nscsjsj|0.93|hsbcjsnc
Thanks in advance
------------------------------------------------------------------------

--
http://mail.python.org/mailman/listinfo/python-list
Jul 24 '08 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.