472,993 Members | 2,473 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,993 software developers and data experts.

unpickle from URL problem

I am on a Windows box.

I pickle a tuple of 2 simple objects with the pickle module.
It pickles fine. It unpickles fine.

I upload to a server.
I try to unpickle from the URL. No luck. Try it:
x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example1'))

I change the filetype to unix. I upload again.
I try to unpickle from the URL. Now it works. Try it:
x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example2'))

Why the difference?

Thank you,
Alan Isaac
Oct 10 '07 #1
6 5532
On Wed, 10 Oct 2007 05:58:51 +0000, Alan Isaac wrote:
I am on a Windows box.

I pickle a tuple of 2 simple objects with the pickle module.
It pickles fine. It unpickles fine.

I upload to a server.
I try to unpickle from the URL. No luck. Try it:
x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example1'))

I change the filetype to unix. I upload again.
I try to unpickle from the URL. Now it works. Try it:
x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example2'))

Why the difference?
Pickles are *binary* files, not text files, so make sure you always treat
them as binary, e.g. opening the files with mode 'rb' and 'wb' and don't
transmit them in text mode over FTP etc.

Ciao,
Marc 'BlackJack' Rintsch
Oct 10 '07 #2
Marc 'BlackJack' Rintsch wrote:
Pickles are *binary* files, not text files
Actually not:
http://docs.python.org/lib/node316.html

These were created with protocol 0.

But my question is about the different outcomes
I observed.

Thank you,
Alan Isaac
Oct 10 '07 #3
Alan Isaac <ai****@american.eduwrites:
I upload to a server.
I try to unpickle from the URL. No luck. Try it:
x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example1'))

I change the filetype to unix. I upload again.
I try to unpickle from the URL. Now it works. Try it:
x1, x2 = pickle.load(urllib.urlopen('http://www.american.edu/econ/notes/hw/example2'))

Why the difference?
The first upload breaks the file. You uploaded it in (presumably
FTP's) text mode, which changes \n -\r\n. But you download it using
http, which specifies no such conversion in the opposite direction.

It is true that the lowest pickle protocol is textual in nature, but
it just means that you can open a file in text mode, write the pickle
into the file, and reliably read it back on the same platform. The
newlines will be converted to the platform newline representation
(such as \r\n on Windows or \r on Mac) upon write, and back to the
original single-byte (\n) representation upon read. But if you feed
the unpickler a string that already contains \r\n, there is nothing to
convert it back to \n.
Oct 10 '07 #4
Hrvoje Niksic wrote:
The first upload breaks the file. You uploaded it in (presumably
FTP's) text mode, which changes \n -\r\n. But you download it using
http, which specifies no such conversion in the opposite direction.
No: I used binary upload both time.
(Unless my ftp client is broken, and I think not.)

The first example simply keeps the Windows eols,
which are present in the pickled file on my Windows machine.
This is the one created by pickle.dump.
This file unpickles just fine.

I altered the 2nd file before upload,
changing to the Unix eol convention.
This also unpickles just fine on my machine,
but in addition the urllib download of this
file unpickles just fine.

Alan Isaac
Oct 10 '07 #5
On Wed, 10 Oct 2007 15:21:06 +0000, Alan Isaac wrote:
Marc 'BlackJack' Rintsch wrote:
>Pickles are *binary* files, not text files

Actually not:
http://docs.python.org/lib/node316.html

These were created with protocol 0.
Actually yes, the docs are wrong. It's a binary file with bytes
constraint to ASCII values. Nevertheless it breaks if you don't open the
files in binary mode and try to use it on a platform that treats line
endings differently.

Ciao,
Marc 'BlackJack' Rintsch
Oct 10 '07 #6
Alan Isaac <ai****@american.eduwrites:
Hrvoje Niksic wrote:
>The first upload breaks the file. You uploaded it in (presumably
FTP's) text mode, which changes \n -\r\n. But you download it using
http, which specifies no such conversion in the opposite direction.

No: I used binary upload both time.
(Unless my ftp client is broken, and I think not.)

The first example simply keeps the Windows eols,
which are present in the pickled file on my Windows machine.
That's why it doesn't work on Unix, the \r's are a problem when not
removed by the platform's IO layer. Because of the newline silliness,
the notion of a "text file" is, unfortunately, quite platform-specific
even when the file is pure ASCII, so pickle protocol 0 being textual
is a bit misleading regarding the resulting file's portability. :-(
Oct 10 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jesse Bloom | last post by:
I keep running into a problem when I use pickle or cPickle to unpickle a python object I have serialized from the database. I read the string for the serialized object. Apparently I am missing an...
7
by: Yun Mao | last post by:
Hi, I'm looking for a way in unpickling, or equivalent, such that can only unpickle (or is limited to) simple data structure, such as number, string, list, tuples. The doc I found...
3
by: draghuram | last post by:
Hi, I have a python script that pickles and unpickles a give object. It works without any problems on windows (the data was pickled on windows first). But when I try to run the script on Linux,...
4
by: Ted Zeng | last post by:
Hi, I store some test results into a database after I use python To pickle them (say, misfiles=) Now I want to display the result on a web page which uses PHP. How could the web page...
11
by: krishnakant Mane | last post by:
hello, I finally got some code to push a pickled list into a database table. but now the problem is technically complex although possible to solve. the problem is that I can nicely pickle and...
10
by: krustymonkey | last post by:
I'm wondering if anyone can help with a workaround for a problem I currently have. I'm trying to set up a prefork tcp server. Specifically, I'm setting up a server that forks children and has them...
2
by: Danny Shevitz | last post by:
Howdy, In my app I need to exec user text that defines a function. I want this function to unpickle an object. Pickle breaks because it is looking for the object definition that isn't in the...
0
by: gdrude | last post by:
I should start of by letting everyone know that I am not an experienced programmer. I have read a number of books on OOP languages and settled on Python to solve this particular problem. ...
4
by: Daniel | last post by:
Hello, I'm trying to build a very simple IPC system. What I have done is create Data Transfer Objects (DTO) for each item I'd like to send across the wire. I am serializing these using...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...
3
SueHopson
by: SueHopson | last post by:
Hi All, I'm trying to create a single code (run off a button that calls the Private Sub) for our parts list report that will allow the user to filter by either/both PartVendor and PartType. On...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.