Hi, I don’t know if anyone would be able to help me with the following problem, I am hoping :D ..

I have looked on the internet but no look.

I have pickled a document into the pickle object, how can I now search the pickled object to remove a certain string of text within the document.

You assistants would be greatly appreciated as I have little knowledge of python.

Member Avatar for masterofpuppets

hi,
not sure if this will help, but here's an example on how to use the pickle module:

import pickle

string = "this is a test of the program"
f = open( "test.txt", "w" )
pickle.dump( string, f )
f.close()

f2 = open( "test.txt", "r" )
string2 = pickle.load( f2 )
newStringList = []
f2.close()

print string2

remove = "test"
for word in string2.split( " " ):
    if word != remove:
        newStringList += [ word ]

newString = ""
for i in newStringList:
    newString += i + " "

print newString

f2 = open( "test.txt", "w" )
pickle.dump( newString, f2 )
f2.close()

>>> 
this is a test of the program
this is a of the program 
>>>

I would not mess with the pickle file directly. Better load (unpickle) the file, do your changes and then dump (pickle) it again.

If the pickled file was created using protocol=0, then you can look at the pickled file with a regular text editor, see if you can find the string of text and carefully remove it. I would do that on a copy of the original file.

Hi, Sorry forgot to say that XML docment will be pickled which is requested through a HTTP request.

Apologies.

  1. Unpickle the XML document into memory
  2. Save it as a plain text file
  3. Edit that file in a text or XML editor
  4. Load that file's contents back in Python
  5. Pickle them again.

I am assuming you know how to pickle and unpickle files, and how to load and save XML documents from/to plain text files.

  1. Unpickle the XML document into memory
  2. Save it as a plain text file
  3. Edit that file in a text or XML editor
  4. Load that file's contents back in Python
  5. Pickle them again.

I am assuming you know how to pickle and unpickle files, and how to load and save XML documents from/to plain text files.

I sort of Know, buta it unsure. first time with python, and finding it a bit confusing.

I sort of Know, buta it unsure. first time with python, and finding it a bit confusing.

To read and write a text file:

content = file(filename, 'rt').read()
file(filename, 'wt').write(content)

See masterofpuppets' response for how to pickle and unpickle stuff.

This should be more than sufficient to get you going.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.