S
sri2097
Hi all, I'm storing number of dictionary values into a file using the
'cPickle' module and then am retrieving it. The following is the code
for it -
# Code for storing the values in the file
import cPickle
book = {raw_input("Name: "): [int(raw_input("Phone: ")),
raw_input("Address: ")] }
file_object = file(database, 'w+')
cPickle.dump(book, file_object)
file_object.close()
# Code for retrieving values and modifiing them.
tobe_modified_name = raw_input("Enter name to be modified: ")
file_object = file(database)
while file_object.tell() != EOFError:
try:
stored_dict = cPickle.load(file_object)
if stored_dict.has_key(tobe_modified_name):
print ("Entry found !")
# I want to modify the values retrieved from the file and
then put it back to the file without duplicate entry.
file_object = file(database, 'a+')
except EOFError:
break
file_object.close()
Now, my problem is after finding the entry in the file, I want to make
changes to the 'values' under the searched 'key' and then insert it
back to the file. But in doing so I'm having duplicate entries for the
same key. I want to remove the previous key and value entry in the file
and key the latest one. How to solve this problem ?
I actually thought of 2 ways -
1) In Java there is something called 'file_pointer' concept where in
after you find the entry you are looking for you move all the entries
below this entry. Then you get the searched entry at the bottom of the
file. After this truncate the file by a certain bytes to remove the old
entry. Can we do this in Python using the file.truncate([size]) method
?
2) Although this is a really crappy way but nevertheless I'll put it
across. First after finding the entry you are looking for in the file,
make a copy of this file without the entry found in the previous file.
Make the changes to the 'values' under this key and insert this into
the second file what you have created. Before exiting delete the first
file.
Are there any more ways to solve my problem ? Any criticisms are
welcome....
'cPickle' module and then am retrieving it. The following is the code
for it -
# Code for storing the values in the file
import cPickle
book = {raw_input("Name: "): [int(raw_input("Phone: ")),
raw_input("Address: ")] }
file_object = file(database, 'w+')
cPickle.dump(book, file_object)
file_object.close()
# Code for retrieving values and modifiing them.
tobe_modified_name = raw_input("Enter name to be modified: ")
file_object = file(database)
while file_object.tell() != EOFError:
try:
stored_dict = cPickle.load(file_object)
if stored_dict.has_key(tobe_modified_name):
print ("Entry found !")
# I want to modify the values retrieved from the file and
then put it back to the file without duplicate entry.
file_object = file(database, 'a+')
except EOFError:
break
file_object.close()
Now, my problem is after finding the entry in the file, I want to make
changes to the 'values' under the searched 'key' and then insert it
back to the file. But in doing so I'm having duplicate entries for the
same key. I want to remove the previous key and value entry in the file
and key the latest one. How to solve this problem ?
I actually thought of 2 ways -
1) In Java there is something called 'file_pointer' concept where in
after you find the entry you are looking for you move all the entries
below this entry. Then you get the searched entry at the bottom of the
file. After this truncate the file by a certain bytes to remove the old
entry. Can we do this in Python using the file.truncate([size]) method
?
2) Although this is a really crappy way but nevertheless I'll put it
across. First after finding the entry you are looking for in the file,
make a copy of this file without the entry found in the previous file.
Make the changes to the 'values' under this key and insert this into
the second file what you have created. Before exiting delete the first
file.
Are there any more ways to solve my problem ? Any criticisms are
welcome....