Reading a file
Gary Herron
gherron at islandtraining.com
Thu Jul 24 16:27:52 EDT 2008
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
More information about the Python-list
mailing list