1

I wrote the below code for writing the contents to the file,

   with codecs.open(name,"a","utf8") as myfile:
         myfile.write(str(eachrecord[1]).encode('utf8'))
         myfile.write(" ")
         myfile.write(str(eachrecord[0]).encode('utf8'))
         myfile.write("\n")`

The above code does not work properly when writing uni-code characters....Even though i am using codecs and doing the encoding. I keep getting the error

UnicodeEncodeError: 'ascii' codec can't encode character u'\xe1' in position 6: ordinal not in range(128)

Can anyone see where i am doing it wrong ?

Edit:

with codecs.open(name,"a","utf8") as myfile:
                    myfile.write(unicode(eachrecord[1]))
                    myfile.write(" ")
                    myfile.write(unicode(eachrecord[0]))
                    myfile.write("\n")

This worked..Thanks for all the quick comments and answers..that really helps ..I did not realize that python has 'unicode' option until you guys told me

4
  • 3
    What type is eachrecord[1]? str is making the mess I guess, use unicode instead. Commented Apr 26, 2012 at 16:12
  • 2
    And I think you do not even have to encode the unicode string, as the file opened using codecs should handle it. Commented Apr 26, 2012 at 16:14
  • eachrecord[1] can be string or a number Commented Apr 26, 2012 at 16:14
  • TypeError: coercing to Unicode: need string or buffer, int found..any suggestions on how to resolve this Commented Apr 26, 2012 at 16:15

1 Answer 1

2

Remove the str() calls. They are doing an implicit encoding with the default encoding (ascii in this case).

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

1 Comment

Thank you, i keep getting this error "AttributeError: 'int' object has no attribute 'encode'" when i remove the str() calls

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.