[Tutor] Pickling files in Python
Hi all I am new to programming and on a very steep curve. I am using Python 3 to learn and I am having trouble understanding exactly what pickling is. I have written a program that asks for a URL and then writes the source code from the URL to a file, then pickles the file I just created. I am not sure why I pickle something as opposed to just saving it as a normal file? I saved the url source code file as a .txt and also the pickled file as .txt. I am not sure of the filetype I should save a pickled file as.When I look at the files (the saved source file and the pickled file) they look the same. I am viewing them in notepad on the Mac. Many thanksJoe Pickle an object- Puzzle 5 # Joe Batt 18 Nov 2011 - http://www.pythonchallenge.com/pc/def/peak.html### import pickleimport urllib def geturlfile(urlfile):#gets the user URL from main opens it and saves it to var fileurlfrom urllib.request import urlopen fileurl=urlopen(urlfile)#opens the file on computer and writes the var fileurl to ithtml_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','w') for line in fileurl.readlines():linestring=line.decode(utf-8) #ensures written in string not bytehtml_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','wb') pickle.dump(filecontents,pickledfile)pickledfile.close() return () # Main programurlfile=input('Please input the URL ')geturlfile(urlfile) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Further pickle problem :0(
Hi all again Thank you to those that have helped me previously with this problem it is appreciated. Thanks to Walter I have actually found the pickle file that I am trying to restore to a variable however I am getting the following error Traceback (most recent call last): File /Users/joebatt/Desktop/python/pickling puzzle 5.py, line 39, in module a=pickle.load(file) File /Library/Frameworks/Python.framework/Versions/3.2/lib/python3.2/encodings/ascii.py, line 26, in decodereturn codecs.ascii_decode(input, self.errors)[0]UnicodeDecodeError: 'ascii' codec can't decode byte 0xd1 in position 8: ordinal not in range(128) The simple program I am using to 'depickle' is file=open('///Users/joebatt/Desktop/banner.p.webarchive','r')a=pickle.load(file)file.close()print (a) I am using Python 3 and working through the Python Challenges on www.pythonchallenge.com, the site is a few years old and I was thinking that possibly it is based around Python 2.x ??? If thats the case is the error due to me using 3 as opposed to 2.x Many thanksJoe___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Stacks and Stack underflow/Stack overflow
Hi All Could some kind soul please explain why you get a stack underflow and a stack overflow. I am getting the following error in Python 3 Traceback (most recent call last): File /Users/joebatt/Desktop/python/pickling puzzle 5.py, line 39, in module a=pickle.load(file)_pickle.UnpicklingError: unpickling stack underflow when I am running the following import picklefile=open('///Users/joebatt/Desktop/banner.p.webarchive','rb')a=pickle.load(file)file.close()print (a) Now I am very much a learner so please correct and explain my misunderstanding. I am visualising my program as taking the 'banner.p.webarchive' and and pushing it onto the stack byte by byte until it reaches the end of the file i.e. A B C --- push C then B then A A B C --- A B C Then the program pops the stack C does what it needs to do to unpickle it then pops B does what it needs to unpickle then A. A -C B ABC My understanding is that the stack underflow means that it is trying to pop from the top of the stack and there is nothing to pop i.e. it is empty. Why though if the stack has been loaded with the file 'banner.p' in my program does it say there is nothing to pop and thus a stack underflow? Also my understanding of the stack overflow is that the stack itself is a finite size and when it has tried to push the file to the stack it didn't fit because it was too big, i.e. in my example if the stack was only big enough for 2 letters and I tried to push ABC it would give a stack overflow because I was trying to push 3. How would I deal with this? Can I make the stack bigger or a way to just push A B pop AB then push C and pop it. Thank you guys Im sorry for probably what are very silly basic questions. Joe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] In a pickle over pickles….Python 3
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 geturlfilepickle.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 fileurlfrom urllib.request import urlopen fileurl=urlopen(urlfile)#opens the file on computer and writes the var fileurl to ithtml_file=open('///Users/joebatt/Desktop/python/puzzle5.txt','w') for line in fileurl.readlines():linestring=line.decode(utf-8) #ensures written in string not bytehtml_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) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Printing with no newline :(
I am learning Python 3 and programming and am very new so please bear with me… I am writing a program to pull out specific characters in a sequence and then print then out. So far so good however when the characters are printed out they pint on separate lines as opposed to what I want, all on the same line. I have tried \n and just , in the pint statement i.e. print(letterGroup[4],) and print(letterGroup[4]\n) and even print(letterGroup[4],/n)…….. Can anyone help and explain please….Thank you for line in file:m = re.search(regexp, line)if m: letterGroup=m.group(0)print(letterGroup[4]) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] Help with re in Python 3
Hi all,Still trying with Python and programming in general…. I am trying to get a grip with re. I am writing a program to open a text file and scan it for exactly 3 uppercase letters in a row followed by a lowercase followed by exactly 3 uppercase letters. ( i.e. oooXXXoXXXooo )If possible could you explain why I am getting EOL while scanning string literal when I try running the following program in Python 3. My program: import reregexp=re.compile(r[a-z]r[A-Z]r[A-Z]r[A-Z]r[a-z]r[A-Z]r[A-Z]r[A-Z]r[a-z]) file=('///Users/joebatt/Desktop/python3.txt','r')for line in file.readlines():if regexp.search(line):print(Found value 3 caps followed by lower case followed by 3 caps)file.close() If possible could you explain why I am getting EOL while scanning string literal when I try running my program in Python 3. Thanks for your help Joe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
[Tutor] A total newbie…sorry
I am just starting to try to learn Python on IDLE on a Mac running Python 3.2.2 (v3.2.2:137e45f15c0b, Sep 3 2011, 17:28:59) and I have come unstuck at the very beginning I tried print HelloSyntaxError: invalid syntax print 'Hello'SyntaxError: invalid syntax print HelloSyntaxError: invalid syntax And as you can see its just saying invalid syntax yet if I run Python in the terminal using the same syntax it works Python 2.7.1 (r271:86832, Jun 16 2011, 16:59:05) [GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwinType help, copyright, credits or license for more information. print HelloHello I can see its a different version but what is the problem? Many thanks and sorry for the VERY basic question :( Joe ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor