Discussion:
[Tutor] find pickle and retrieve saved data
Quiles, Stephanie
2015-08-03 03:04:08 UTC
Permalink
how do i go about being able to add a feature to search for individual entries that have been saved into that dictionary or else tell me that the name I entered is not found?

Here is the code that i have so far…

import pickle
def main():
infile = open("emails.dat", "rb")
emails = pickle.load(infile)
infile.close()
name_search = input("Enter a name in the file for info: ")

for name in emails:

if name[0] == name_search:
print("This is the info: ", info)
return emails
else:
print("Entry not Found! Try again.")
main()


thanks for all the help and suggestions this is really helping me in trying to figure this out!

Stephanie
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription opti
Alan Gauld
2015-08-03 07:11:45 UTC
Permalink
On 03/08/15 04:04, Quiles, Stephanie wrote:

> def main():
...
> name_search = input("Enter a name in the file for info: ")
>
> for name in emails:
> if name[0] == name_search:
> print("This is the info: ", info)

What is info? Is it supposed to be name? or name[1:]?
Its not set anywhere in your code.

> return emails

Notice the return is outside the if block.
So you always return on the first element of the for loop.

> else:
> print("Entry not Found! Try again.")

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Quiles, Stephanie
2015-08-03 12:13:29 UTC
Permalink
I'm trying to tell it to print everything under that particular name. I would have to def info, correct? But set it equal to what to make it work?

Stephanie Quiles
Sent from my iPhone

> On Aug 3, 2015, at 3:12 AM, Alan Gauld <***@btinternet.com> wrote:
>
>> On 03/08/15 04:04, Quiles, Stephanie wrote:
>>
>> def main():
> ...
>> name_search = input("Enter a name in the file for info: ")
>>
>> for name in emails:
>> if name[0] == name_search:
>> print("This is the info: ", info)
>
> What is info? Is it supposed to be name? or name[1:]?
> Its not set anywhere in your code.
>
>> return emails
>
> Notice the return is outside the if block.
> So you always return on the first element of the for loop.
>
>> else:
>> print("Entry not Found! Try again.")
>
> --
> Alan G
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
> http://www.amazon.com/author/alan_gauld
> Follow my photo-blog on Flickr at:
> http://www.flickr.com/photos/alangauldphotos
>
>
> _______________________________________________
> Tutor maillist - ***@python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Quiles, Stephanie
2015-08-04 22:09:27 UTC
Permalink
I am still struggling with this one.

Here is my code to retrieve data from emails.dat file

def main():
found = False

search = input("Enter a name in the file for info: ")

infile = open("emails.dat", "r")
name = infile.readline()

while name != '':
email1, email2, phone, phone2 = (infile.readline())
name = name.rstrip("\n")

if name == search:
print("name: ", name)
print("Email1, alternate email, phone, alternate phone", email1, email2, phone, phone2)
print()
found = True

name = infile.readline()
infile.close()

if not found:
print("That name is not found in file.")


main()

This is the error i am getting:

enter a name in the file for info: sarah
Traceback (most recent call last):
File "/Users/stephaniequiles/Downloads/findemails.py", line 28, in <module>
main()
File "/Users/stephaniequiles/Downloads/findemails.py", line 9, in main
name = infile.readline()
File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/codecs.py", line 319, in decode
(result, consumed) = self._buffer_decode(data, self.errors, final)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

Process finished with exit code 1


> On Aug 3, 2015, at 8:13 AM, Quiles, Stephanie <***@albright.edu> wrote:
>
> I'm trying to tell it to print everything under that particular name. I would have to def info, correct? But set it equal to what to make it work?
>
> Stephanie Quiles
> Sent from my iPhone
>
>> On Aug 3, 2015, at 3:12 AM, Alan Gauld <***@btinternet.com> wrote:
>>
>>> On 03/08/15 04:04, Quiles, Stephanie wrote:
>>>
>>> def main():
>> ...
>>> name_search = input("Enter a name in the file for info: ")
>>>
>>> for name in emails:
>>> if name[0] == name_search:
>>> print("This is the info: ", info)
>>
>> What is info? Is it supposed to be name? or name[1:]?
>> Its not set anywhere in your code.
>>
>>> return emails
>>
>> Notice the return is outside the if block.
>> So you always return on the first element of the for loop.
>>
>>> else:
>>> print("Entry not Found! Try again.")
>>
>> --
>> Alan G
>> Author of the Learn to Program web site
>> http://www.alan-g.me.uk/
>> http://www.amazon.com/author/alan_gauld
>> Follow my photo-blog on Flickr at:
>> http://www.flickr.com/photos/alangauldphotos
>>
>>
>> _______________________________________________
>> Tutor maillist - ***@python.org
>> To unsubscribe or change subscription options:
>> https://mail.python.org/mailman/listinfo/tutor

_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Alan Gauld
2015-08-05 00:01:35 UTC
Permalink
On 04/08/15 23:09, Quiles, Stephanie wrote:

> def main():
> found = False
>
> search = input("Enter a name in the file for info: ")
>
> infile = open("emails.dat", "r")
> name = infile.readline()
> while name != '':
> email1, email2, phone, phone2 = (infile.readline())
> name = name.rstrip("\n")

You should probably do this immediately after reading the name

> if name == search:
> print("name: ", name)
> print("Email1, alternate email, phone, alternate phone", email1, email2, phone, phone2)
> print()
> found = True

Note that if name does not equal search you will go round this loop
forever (or at least until you finish reading the file) because you
don't change the name.

> name = infile.readline()

I suspect this line was supposed to be inside the while loop?


See if that helps?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Oscar Benjamin
2015-08-05 21:40:13 UTC
Permalink
On 4 August 2015 at 23:09, Quiles, Stephanie
<***@albright.edu> wrote:
> I am still struggling with this one.

Hi Stephanie, Alan has already raised a few issues with your code so
I'm just going to address the one that's showing in your error
message.

These two lines are generating the error message:

> infile = open("emails.dat", "r")
> name = infile.readline()
>
> This is the error i am getting:
>
> enter a name in the file for info: sarah
> Traceback (most recent call last):
> File "/Users/stephaniequiles/Downloads/findemails.py", line 28, in <module>
> main()
> File "/Users/stephaniequiles/Downloads/findemails.py", line 9, in main
> name = infile.readline()
> File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/codecs.py", line 319, in decode
> (result, consumed) = self._buffer_decode(data, self.errors, final)
> UnicodeDecodeError: 'utf-8' codec can't decode byte 0x80 in position 0: invalid start byte

The interpreter is assuming that your file 'emails.dat' is a text file
using the utf-8 encoding. This is because you didn't specify an
encoding when opening the file. To specify that the encoding is e.g.
ascii you would do:

infile = open('emails.dat', 'r', encoding='ascii')

The error occurs because the bytes read from the file are not valid
for utf-8. What program did you use to write the emails.dat file? Does
it look reasonable when you open it in a text editor e.g. your code
editor or notepad?

If you show the output of the following command then someone may be
able to guess the encoding that you should be using for this file:

infile = open('emails.dat', 'rb') # b for binary mode
print(repr(infile.read(100)))

--
Oscar
_______________________________________________
Tutor maillist - ***@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor
Loading...