469,906 Members | 2,195 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,906 developers. It's quick & easy.

Re: Reading a file

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
0 771

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Xah Lee | last post: by
19 posts views Thread by Lionel B | last post: by
4 posts views Thread by Oliver Knoll | last post: by
6 posts views Thread by Rajorshi Biswas | last post: by
7 posts views Thread by John Dann | last post: by
1 post views Thread by Waqarahmed | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.