1

I am basically parsing data from XML using SAX Parser in Python.

I am able to parse and print. However I wanted to put the data to a text file.

sample:

def startElement(self, name, attrs): file.write("startElement'"+ name + " ' ") 

While trying to write some text to a test.txt with above sample code, I get below error:

TypeError: descriptor 'write' requires a 'file' object but received a 'unicode' 

Any help is greately appreciated.

1 Answer 1

2

You are not using an open file. You are using the file type. The file.write method is then unbound it expected an open file to be bound to:

>>> file <type 'file'> >>> file.write <method 'write' of 'file' objects> >>> file.write(u'Hello') Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: descriptor 'write' requires a 'file' object but received a 'unicode' 

If you have an already opened file object, then use that; perhaps you have an attribute named file on self:

self.file.write("startElement'" + name + " ' ") 

but take into account that because name is a Unicode value you probably want to encode the information to bytes:

self.file.write("startElement'" + name.encode('utf8') + " ' ") 

You could also use io.open() function to create a file object that'll accept Unicode values and encode these to a given encoding for you when writing:

file_object = io.open(filename, 'w', encoding='utf8') 

but then you need to be explicit about always writing Unicode values and not mix byte strings (type str) and Unicode strings (type unicode).

Sign up to request clarification or add additional context in comments.

7 Comments

@Dinakar: but you didn't put that in a place that makes it a global. Your startElement method looks up the name file as a global, and finds the built-in.
@Dinakar: in other words, you'll need to show us more code if you want us to help find why the method doesn't find your file.
@Martjin Pieters: I think this is helping. But i get below error now: file = open("out.txt", "w", "utf-8") TypeError: an integer is required
I am a learner. Can you please elaborate on "but then you need to be explicit about always writing Unicode values and not mix byte strings (type str) and Unicode strings (type unicode)."
@Dinakar: you are missing the encoding keyword. You are trying to set the buffer parameter instead.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.