On 11/18/2011 11:12 AM, Joe Batt wrote:
Hi All,Sorry to trouble you all again with more nooby problems! Only been
programming a week so still all a haze especially since self taught…..
I am opening a url and saving it to a file on my computer, then I am trying to
pickle it. I have written the url file to a file on my computer then opened it
and assigned the contents to a var 'filecontents' and tried to pickle it it is
giving the error:
Traceback (most recent call last): File "/Users/joebatt/Desktop/python/pickling puzzle 5.py",
line 39, in<module> geturlfile(urlfile) File "/Users/joebatt/Desktop/python/pickling
puzzle 5.py", line 28, in geturlfile pickle.dump(filecontents,pickledfile)TypeError: must be str,
not bytes
Yet I tested the variable filecontents using print (type(file contents)) and
it is saying that it is a str yet the error seems to indicate it is a byte, can
anyone point me in the right direction please?
(I know my coding is very untidy and verbose, sorry I am very new and at the
moment I have to think in baby steps with little knowledge!)
Joe
#################################################################### Pickle an
object- ###################################################################
import pickleimport urllib
def geturlfile(urlfile):#gets the user URL from main opens it and saves it to var fileurl
from urllib.request import urlopen fileurl=urlopen(urlfile)#opens the file on
computer and writes the var fileurl to it
html_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','w') for line in
fileurl.readlines(): linestring=line.decode("utf-8") #ensures written in
string not byte html_file.write(linestring) html_file.close() #closes the
puzzle file#just prints file to check its correct
html_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','r')
filecontents=html_file.read() html_file.close() print (filecontents) print
(type(filecontents))
#pickles the file filecontents containing the url file
pickledfile=open('///Users/joebatt/Desktop/python/pickledpuzzle5.txt','w')
pickle.dump(filecontents,pickledfile) pickledfile.close()
return ()
# Main programurlfile=input('Please input the URL ')geturlfile(urlfile)
You forgot to post in text mode, so everything ran together.
--
DaveA
_______________________________________________
Tutor maillist - Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor