Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I am using python and XMLBuilder, a module I downloaded off the internet (pypi). and in the end it returns an object, that works like a string (I can do print(x)) but when I use file.write(x) it crashes and does says that there was an error in the XMLBuilder module. I LOVE the XMLBuilder module aside from that and I do not want to know how I "should do it with "+x+" module" I am just wondering how I can convert the object it returns into a string. edit, I have confirmed that I am writing to the file write I have already tried x = y although, as I thought, it just creates a pointer, and also x=x+" " put I still get an error. Also it returns an string like object with "\n". Thanks in advance.

share|improve this question
try file.write(str(x)) – Aamir Adnan 2 days ago
Maybe the file.write() function do not call the overloaded str function of the object. Did you try file.write(str(x)) ? – fodma1 2 days ago

4 Answers

The Library has __str__ defined:

def __str__(self):
    return tostring(~self, self.__document()['encoding'])

So you just need to use str(x):

file.write(str(x))
share|improve this answer
file.write(str(x))

will likely work for you.

Background information: Most types have a function __str__ or __repr__ (or both) defined. If you pass an object of such a type to print, it'll recognize that you did not pass a str and try to call one of these functions in order to convert the object to a string.

However, not all functions are as smart as print and will fail if you pass them something that is not a string. Also string concatenation does not work with mixed types. To work with these functions you'll have to convert the non-string-type objects manually, by wrapping them with str(). So for example:

x = str(x)+" "

This will create a new string and assign it to the variable x, which held the object before (you lose that object now!).

share|improve this answer
thank you this is what I was looking for, and nicely explained too. – user2488921 yesterday
cool! then I'd be happy if you'd accept my answer. (click on the check mark) – Alexander Tobias Heinrich yesterday

I'm not quite sure what your question is, but print automatically calls str on all of it's arguments ... So if you want the same output as print to be put into your file, then myfile.write(str(whatever)) will put the same text in myfile that print (x) would have put into the file (minus a trailing newline that print puts in there).

share|improve this answer

When you write:

print myObject

The __repr__ method is actually called.

So for example you could do: x += myXMLObject.__repr__() if you want to append the string representation of that object to your x variable.

share|improve this answer
1  
print calls __repr__ only when __str__ is not found. – Ashwini Chaudhary 2 days ago

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.